- Home /
AnimationEvent.animationState help: NullReferenceException
Okay so I added this event to the timeline of my animation at a certain frame. I want to know what animation state had sent this event. Except I get this error:
NullReferenceException
ThisCScript.someSuch (UnityEngine.AnimationEvent ae) (at Assets/Dude/ThisCScript.cs:94)
Here's the function that is being called by the event.
public void someSuch (AnimationEvent ae)
{
AnimationState state = ae.animationState;
string name = state.name;
print (name);
print (ae.floatParameter);
print (ae.intParameter);
print (ae.stringParameter);
}
If I comment out the string name = state.name part, and also the print(name) parts, the code works fine. It outputs the parameters just fine.
I even tried to use the debugger. When I looked at the value for ae.animationState, it said "object reference not set to an instance of an object". I am not sure what causes this though. I mean the script is most certainly called by an animation. Can anybody tell me what I'm doing wrong?
I am experiencing the same problem. AnimationEvent.animationState is always null whether responding to callbacks from $$anonymous$$ecanim or the legacy animation system, it seems. The documentation (https://docs.unity3d.com/Documentation/ScriptReference/AnimationEvent-animationState.html) suggests that this may occur if "the method is called outside of an animation event callback", but I am sure that I am calling the method from within said callback.
I am having the exact same problem. Is this a bug in unity or has anyone found out what might be the problem?
In my specific case I am looking for a method to find out whether the event is fired by a state which we are transitioning (crossfading) away from.
Best regards, Jakob
Answer by Bodrp · Mar 10 at 04:43 PM
If you are using the Animator component, and not the legacy Animation component, you should use animatorStateInfo property of the AnimationEvent instead of animationState. animatorStateInfo will not be null when the function is called from an Animator's animation event.
More information about AnimatorStateInfo can be found here: https://docs.unity3d.com/ScriptReference/AnimatorStateInfo.html
Your answer