x


Animation Error "Null reference exception"?

I am building a FPS (not the tutorial) and I built a series of animations for a gun model using Maya (a .mb file). I have imported the animations, and written a basic script that runs the bolt slide animation when you click the mouse (and fire the weapon) here is the script:

function Update ()
{
    if(Input.GetButtonDown("Fire1"))
    {   
        animation["Element 0"].wrapMode = WrapMode.Once;
        animation.Stop();
    }
}

however, when I run this script, I get a "Null reference exception" message in the debug window, what is the error?

Thanks

more ▼

asked Feb 17 '10 at 03:17 PM

Slim gravatar image

Slim
14 3 3 4

Which line exactly are you getting the NullReferenceException on?

Feb 17 '10 at 03:53 PM jashan

Line 5: animation["Element 0"].wrapMode = WrapMode.Once;

Feb 17 '10 at 05:38 PM Slim
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Wow, I just used the animation.Play command and it works fine, case solved. Now, I wonder what the difference between wrapMode and play is...

more ▼

answered Feb 18 '10 at 08:41 PM

Slim gravatar image

Slim
14 3 3 4

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

Most likely there is no animation by the name of "Element 0". Or, the game object that you have attached this script to doesn't have an animation component attached to it at all.

more ▼

answered Feb 17 '10 at 03:53 PM

jashan gravatar image

jashan
10.1k 25 41 116

Well, "element 0" is the first element of the animation component of gameObject M1.Reload (my weapon) M1.Reload is also the true name of the animation...

Feb 17 '10 at 05:08 PM Slim

"element 0" or "Element 0" - in your code, you have "Element 0" (upper-case E), and if it's actually called "element 0" (lower-case e) that won't work as keys in dictionaries are case-sensitive. So that might be it. I'm not perfectly sure I understand you correctly, though - if the name of the animation is "M1.Reload", it would have to be animation[M1.Reload"] instead. You should also be able to access the animation via animation[0] though (that would be the first animation component).

Feb 18 '10 at 08:56 AM jashan

I do not believe it is the name that is the issue, I simplified the game quite a bit and made it so its supposed to only do one thing when the mouse is clicked, run "M1.Reload" But even with this simple program, while I don't get any error message, it simply won't play the animation. When I check the play automatically box in the animation component of the m1 gameObject, the animation plays, which proves that the animation IS there...

Feb 18 '10 at 08:29 PM Slim

*play on start, my bad.

Feb 18 '10 at 08:30 PM Slim
(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:

x5099
x3807
x198

asked: Feb 17 '10 at 03:17 PM

Seen: 3496 times

Last Updated: Feb 17 '10 at 04:40 PM