NullReferenceException: Object reference not set to an instance of an object (60890)

HOw can i fix this error??

var walkAcceleration : float = 5;
var cameraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontalMovement : Vector2;

function Update ()
{
horizontalMovement = Vector2( rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;

transform.rotation = Quaternion.Euler(0,cameraObject.GetComponent(MouseLookScript).currentYRotation,0);
rigidbody.AddRelativeForce(Input.GetAxis(“Horizontal”) * walkAcceleration, 0, Input.GetAxis(“Vertical”) * walkAcceleration);
}

Firstyoushouldformatthecodeasthisisunreadable. Use 101 010 button.

I'm looking forward to when these forum API's will automatically format code.

Start by finding out which object is null.

If you double click on the error in the console window it will bring up your code with the line the error is on highlighted. It will be a variable on that line that is initialized properly.

Do you have a rigidbody component?

2 Answers

2

Either cameraObject is not set in the inspector, or the cameraObject that is being used doesn’t have a MouseLookScript attached, or the object that this script is attached to doesn’t have a rigidbody.

:O I overlooked the MouseLookScript, I am not paying enough attention to the codes

Either the gameobject this script is attached to do not have a rigidbody component or you forgot to set the var cameraObject : GameObject in your inspector.