x


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?

more ▼

asked Jul 08 '11 at 06:35 PM

blackmethod gravatar image

blackmethod
63 24 28 33

I tested this script and it gave "Missing 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?

Jul 08 '11 at 07:08 PM aldonaletto

Eh em. One of my helpers clearly came in and deleted the rigidbody... Sorry for uhm. Wasting time.

Jul 08 '11 at 07:39 PM blackmethod

Even after I fixed the rigidbody I get the same error.

Jul 08 '11 at 07:42 PM blackmethod
(comments are locked)
10|3000 characters needed characters left

6 answers: sort newest

This error means that some variable is empty, but at runtime the script is trying to use the thing that variable should be referencing to. It happens often when you forget to drag some object to an Inspector variable - like target in camera following scripts. Honestly, in your script the suspect #1 should be the Camera.main variable, but there are evidences of its innocence: the message error indicates line 7, and Camera.main is referenced in line 6 (at least in the fragment above), not to mention the fact that you surely would notice if there were no camera in the scene. In line 7, the only variable referenced is rigidbody, but when it's empty Unity usually shows a different error message: (Missing Component Exception: There's no 'Rigidbody'...). If the rigidbody has a Joint, its Connected Body variable must be defined, or you may get an error.

more ▼

answered Jul 09 '11 at 12:56 AM

aldonaletto gravatar image

aldonaletto
41.3k 16 42 195

All it's controlling is a ball that's supposed to roll. The ball is rigidbodied. The camera is attached, so i'm completely confused as to what is causing this error.

Jul 09 '11 at 02:03 PM blackmethod
(comments are locked)
10|3000 characters needed characters left

This question is still unanswered. Can someone please help me out.

more ▼

answered Jul 08 '11 at 10:50 PM

blackmethod gravatar image

blackmethod
63 24 28 33

(comments are locked)
10|3000 characters needed characters left

Try to use Camera.mainCamera Instead Camera.main.

I use Camera.mainCamera on my scripts.

more ▼

answered Jul 08 '11 at 07:15 PM

Borgo gravatar image

Borgo
998 9 13 25

(comments are locked)
10|3000 characters needed characters left

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); } }`

more ▼

answered Jul 08 '11 at 07:03 PM

Sparkplug94 gravatar image

Sparkplug94
1 1 1 1

Did, and I still end up with the same error.

Jul 08 '11 at 07:47 PM blackmethod
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 08 '11 at 06:58 PM

Borgo gravatar image

Borgo
998 9 13 25

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1943
x394
x262
x193
x128

asked: Jul 08 '11 at 06:35 PM

Seen: 1301 times

Last Updated: Jul 09 '11 at 02:03 PM