- Home /
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?
I tested this script and it gave "$$anonymous$$issing Component Exception: There's no 'Rigidbody'... at line: 7". I had no rigidbody, of course, just to test the script. With a rigidbody added, no errors were flagged. Just a shoot in the dark: do you have a main camera?
Eh em. One of my helpers clearly came in and deleted the rigidbody... Sorry for uhm. Wasting time.
Even after I fixed the rigidbody I get the same error.
Answer by Sparkplug94 · Jul 08, 2011 at 06:47 PM
Don't you need to tell Unity which rigidbody you're referencing? Rigidbody is a component of a Game Object, I believe.
like, Machine1.rigidbody.AddRelativeTorque
Answer by Borgo · Jul 08, 2011 at 06:58 PM
Like Sparkplug94 says, to this script works, its need to be attached to an object that have a rigidbody attached.
Anyway, why are you using Input.GetButton to keys(letters)? That letters are mapped in Edit > Project Settings > Input? If no, the best way to use Keys is Input.GetKey.
Answer by Sparkplug94 · Jul 08, 2011 at 07:03 PM
Borgo is correct. Try this:` 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.GetKey("w")) {
rigidbody.AddRelativeTorque (2, 0, 0);
rigidbody.AddRelativeForce (2, 0, 0); }
if (Input.GetKey("s")) {
rigidbody.AddRelativeTorque (-2, 0, 0); rigidbody.AddRelativeForce (-2, 0, 0); }
if (Input.GetKey("a")) { rigidbody.AddRelativeTorque (0, 0, -2); rigidbody.AddRelativeForce (0, 0, -2); }
if (Input.GetKey("d")) { rigidbody.AddRelativeTorque (0, 0, 2); rigidbody.AddRelativeForce (0, 0, 2); } }`
Answer by Borgo · Jul 08, 2011 at 07:15 PM
Try to use Camera.mainCamera Instead Camera.main.
I use Camera.mainCamera on my scripts.
Answer by blackmethod · Jul 08, 2011 at 10:50 PM
This question is still unanswered. Can someone please help me out.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                