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

Hi there…
I’ve wrote this code

var MainCamera : Camera;
@HideInInspector
var targetXRotation: float;
@HideInInspector
var targetYRotation: float;
@HideInInspector
var targetXRotationV: float;
@HideInInspector
var targetYRotationV: float;

var rotateSpeed : float = 0.3;
var holdHight : float = -0.5;
var holdSide : float = 0.5;

function Update ()
{
    transform.position= MainCamera.transform.position + (Quaternion.Euler(0,targetYRotation,0)* Vector3(holdSide,holdHight,0));
    tragetXRotation = Mathf.SmoothDamp(targetXRotation,MainCamera.GetComponent(MouseLookScript).xRotation,targetXRotationV,rotateSpeed);
    tragetYRotation = Mathf.SmoothDamp(targetYRotation,MainCamera.GetComponent(MouseLookScript).YRotation,targetYRotationV,rotateSpeed);
}

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 :slight_smile:

This has been asked hundreds of times before. Google helps.

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.

Iam sorry this is the first time i ask thing in unity forum
Thanks for your Replay i will seach

You mean change variable of (MainCamera) right?

Check to make sure that the MainCamera variable is set