- Home /
Animator.Play Not Playing Animation - Unity 5
I have an issue with Animator.Play. I have set up an animation the same way I have set up other successful animations that are currently working. Referring to the code below, I am calling Animator.play from the same game object that contains the animator and my animation. When I do this, the animation does not play. Placing a call to my Animate() function does not play my animation. OnAnimationStarted and OnAnimationFinished are both events triggered from within my animation on the first frame and the last frame respectively. I see the "Calling Play Animation" Debug line in the console, and I have confirmed through Visual Studio debugging that the animation exists in my Animator component and that it is named / spelled correctly in code. I do not see any "Animation Started" or "Animation Finished" lines in the console, and my animation simply does not play when called. I use this exact same method elsewhere in my project, and the animation plays and triggers the events as expected. I am at a loss as to why this implementation does not work the same way as the others. Does anybody have any insight into this issue?
Animator m_animator;
public void Animate()
{
Debug.Log("Calling Play Animation");
m_animator = this.gameObject.GetComponent<Animator>();
m_animator.Play("AwardAnimation");
}
void OnAnimationStarted()
{
Debug.Log("Animation Started");
}
void OnAnimationFinished()
{
Debug.Log("Animation Finished.");
}
I have not heard of/used OnAnimationStarted or OnAnimationFinished methods. Are these proprietary to some sort of plug-in you're running? Are you calling these from somewhere else? As far as I know, they're not built into Unity/$$anonymous$$ono's API (can't find any reference) but I could be wrong.
$$anonymous$$aybe you have forgot to set the animation as legacy if you are not using the animator
Is this not considered using the animator?
m_animator = this.gameObject.GetComponent<Animator>();
m_animator.Play("AwardAnimation");
Answer by shloob · Apr 29, 2015 at 02:36 PM
Sorry for not being clear on that! To clarify, the animation window in Unity allows you to trigger events at certain frames of animation. The events are really just calls to functions defined in my code where I have calls to Debug.Log just to tell me if they were getting called or not. In my case the animation does not play, and as a result the events that I defined in the animation editor window are not called when I call
m_animator = this.gameObject.GetComponent();
and then
m_animator.Play("AwardAnimation");