- Home /
NullReferenceException: Object reference not set to an instance of an object
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.
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.
Answer by kru · Jan 20, 2013 at 02:53 AM
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 $$anonymous$$ouseLookScript, I am not paying enough attention to the codes
Answer by Chronos-L · Jan 20, 2013 at 02:01 AM
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.
Your answer
Follow this Question
Related Questions
Passing information error 0 Answers
Object getting destroyed ? 1 Answer
How to check if something is null? 3 Answers