Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error " Object reference not set to an instance of an object"

Discussion in 'Scripting' started by Andrew5, Dec 19, 2014.

  1. Andrew5

    Andrew5

    Joined:
    Dec 19, 2014
    Posts:
    3
    Hi there....
    I've wrote this code
    Code (JavaScript):
    1. var MainCamera : Camera;
    2. @HideInInspector
    3. var targetXRotation: float;
    4. @HideInInspector
    5. var targetYRotation: float;
    6. @HideInInspector
    7. var targetXRotationV: float;
    8. @HideInInspector
    9. var targetYRotationV: float;
    10.  
    11. var rotateSpeed : float = 0.3;
    12. var holdHight : float = -0.5;
    13. var holdSide : float = 0.5;
    14.  
    15. function Update ()
    16. {
    17.     transform.position= MainCamera.transform.position + (Quaternion.Euler(0,targetYRotation,0)* Vector3(holdSide,holdHight,0));
    18.     tragetXRotation = Mathf.SmoothDamp(targetXRotation,MainCamera.GetComponent(MouseLookScript).xRotation,targetXRotationV,rotateSpeed);
    19.     tragetYRotation = Mathf.SmoothDamp(targetYRotation,MainCamera.GetComponent(MouseLookScript).YRotation,targetYRotationV,rotateSpeed);
    20. }
    and when i started test the game this error appeared to me "Object reference not set to an instance of an object" and i didn't understand this error
    please any one tell me what this error mean and if any thing in this code wrong tell me about it :)
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    This has been asked hundreds of times before. Google helps.
     
  3. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    You get this error when you are trying to use a variable that is supposed to point to an object but instead is pointing to noting.

    In your code it looks like the only object that you are using is a camera. I'm guessing that the reason that you're getting the error is because the camera variable (MainCamera) has not been set.
     
  4. Andrew5

    Andrew5

    Joined:
    Dec 19, 2014
    Posts:
    3
    Iam sorry this is the first time i ask thing in unity forum
    Thanks for your Replay i will seach
     
  5. Andrew5

    Andrew5

    Joined:
    Dec 19, 2014
    Posts:
    3
    You mean change variable of (MainCamera) right?
     
  6. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    Check to make sure that the MainCamera variable is set