NullReferenceException on animation clips

so i added a load of animation clips to one of my objects, and my script has no problem with all but one of them. even clips that are referenced in the script but are not added to the object cause no problem, but the "Swipe A" clip causes a "NullReferenceException" error.

i always though that "NullReferenceException" meant that the thing i was referencing in my script couldn't be found, but i've checked and they are named exactly the same, in the same object. there is no reason i can find for this error.

is there some facet of "NullReferenceException" that i'm not understanding, or is this an know bug? any help gratefully received. thanks in advance.

NullReferenceException means the object referenced doesn't exist. It's not exclusive to scripting. If you deleted the actual animation file from your object you'll have to reload the object.

You'll also have to specify and link each animation file to the in-scene object, in the Animations[] array in the inspector.

As @Benproductions1 mentioned, it’s related to an animation clip

I am using Animation, not an Animator component, and had a similar issue.

Referencing an animation from script through the operator told me the clip didn’t exist - null reference exception. Surely it did! it was assigned into the array of an Animation component, it must have worked.

Turns out the animation clip was created incorrectly, and was causing troubles. The way I created it was by duplicating some other Animation clip, trying to save myself some time.

So, to fix this I selected all the properties (shift left click to select several) in the Animation window, Ctrl+C, then deleted this buggy animation clip. Afterwards, right clicked in the same folder, create → Animation clip.

Opened it in the Animation window, ctrl+V to paste the copied properties and it worked fine - my Animation component was able to see the new clip

Hey people. Just stumbled over this because I had the issue myself (wanted to set animation time by code).

If you want to do this in a newer Unity version you’re going to have to set the animation clip as “Legacy”. I’d presume this is what happened when @benproductions1 created his animation clip in the alternate way.

You can do this by switching the editor to Debug-Mode and then simply setting the flag.