- Home /
Question by
blackmethod · Jul 08, 2011 at 06:14 PM ·
NullReferenceException Error
When I try to run my script I get this error:
NullReferenceException Movementscript.FixedUpdate () (at Assets/Movementscript.js:7)
Here is my script:
var speed=20.0;
function FixedUpdate () {
var torque=Vector3(Input.GetAxisRaw("Vertical"), 0, -Input.GetAxisRaw("Horizontal"));
if(torque.magnitude > 0.0){
torque=Camera.main.transform.TransformDirection(torque);
rigidbody.angularVelocity=Vector3.Lerp(rigidbody.angularVelocity, torque * 2, 0.3);
}
if (Input.GetButton("W"))
{
rigidbody.AddRelativeTorque (2, 0, 0);
rigidbody.AddRelativeForce (2, 0, 0);
}
if (Input.GetButton("S"))
{
rigidbody.AddRelativeTorque (-2, 0, 0);
rigidbody.AddRelativeForce (-2, 0, 0);
}
if (Input.GetButton("A"))
{
rigidbody.AddRelativeTorque (0, 0, -2);
rigidbody.AddRelativeForce (0, 0, -2);
}
if (Input.GetButton("D"))
{
rigidbody.AddRelativeTorque (0, 0, 2);
rigidbody.AddRelativeForce (0, 0, 2);
}
}
I don't see any problem with the script, so can someone explain to me what's wrong?
Comment
Answer by Eli-Davis · Jul 08, 2011 at 06:22 PM
Check if the object you are trying to move has a rigid body attached to it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612135433im_/https://answers.unity.com/themes/thub/images/avi.jpg)