Question by
nishantcoolvns · Feb 22, 2016 at 02:26 PM ·
null reference exception
null reference Exception object reference not set i did not understand the prob. please help me
public class PlayerMovment : MonoBehaviour {
private Rigidbody rb;
void start()
{
//accesing comppnent
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
///getting input from the player
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
//calculating the values
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
//adding the input to the player
rb.AddForce(movement);
}
}
Comment
Answer by jgodfrey · Feb 22, 2016 at 03:13 PM
I assume the problem is in this line?
rb.AddForce(movement)
If that's the case, then your "rb" variable doesn't contain a valid reference to a Rigidbody. Are you sure the gameobject this script is attached to has a Rigidbody component?
Ah, wait... I bet the problem is with your "start" method. That should be "Start" (note the capital "S").
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 1 Answer
NullReferenceException in 20 says " Object Reference not set to instance of object Please help 1 Answer
NullReferenceException after save the Scene and Project 0 Answers
Null reference exception to a non-null text object? 1 Answer