GetCurrentAnimatorStateInfo(0).IsName() returns false immediately after playing

…unless I explicitly update the Animator twice:

Animator m_anim = ...;
m_anim.Play("GenericSpinReveal");
m_anim.Update(Time.smoothDeltaTime);
m_anim.Update(Time.smoothDeltaTime);
// Assert test passes only if I have the two  updates above.
DebugUtils.Assert(m_anim.GetCurrentAnimatorStateInfo(0).IsName("Base.GenericSpinReveal"));

Any way around this? Needless to say this breaks a lot of basic logic you might want to do. :shock:

1496634--83954--$Screen Shot 2014-01-25 at 10.14.00 PM.png

1 Like

Nobody?

I found the same issue, and used your hack to get around this bug (THANK YOU!!!).

Btw, it also works doing m_anim.Update(0f), so I don’t think it would break anything.

This is currently a very weird issue for me.

What was the solution?

Edit: Besides using Update…
Thanks,
jrDev

The issue seemed to be that isName() would still match against its starting state for a few frames(?) after the animation was supposed to be triggered.

My code was doing something like

animator.SetTrigger('hide');
while(animator.GetCurrentAnimatorStateInfo(0).IsName("Hiding")){
  yield return null;
}

But I have switched it to

animator.SetTrigger('hide');
while(!animator.GetCurrentAnimatorStateInfo(0).IsName("Hidden")){
  yield return null;
}

and it’s working just fine. If you need to match IsName instead of being able to check an exit condition, I would have a second while loop yielding until IsName returns true once, then another yielding until IsName no longer passes.

It’s always funny seeing the stupid hacks people have to come up with to accomplish even the simplest of tasks with Mecanim. You tell it to play an animation and you just have to hope that it will do what you want at some point in the future.

3 Likes

So I’m in Unity 2020.3.31f1 and I have a similar (but inverse of this) problem; the Animator window shows I’m in a completely different animation state than what I’m testing IsName against in code, but I’m getting a false positive, resulting me being stuck forever in a while loop

//wait for the animation to start
while (!animator.GetCurrentAnimatorStateInfo(0).IsName(animName))
yield return null;

//then wait for the animation to finish
while (
animator.GetCurrentAnimatorStateInfo(0).IsName(animName)
{
//stuck here forever
yield return null;
}```

Has this bug existed for 7+ years and it's just never been fixed?

Still broken. Awesome work, Unity.

1 Like

Indeed. My workaround/hack is to reference the script attached to the Animator in the State’s Behaviour. The script object (AnimatorStateController) cannot be set in the Inspector, so must be referenced in code:

public class WalkBehaviour : StateMachineBehaviour
{
    AnimatorStateController aniController;

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (aniController == null)
            aniController = animator.gameObject.GetComponentInChildren<AnimatorStateController>();
        aniController.UpdateWalkAnimationState(true);

    }

       override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        aniController.UpdateWalkAnimationState(false);

    }

Then in the AnimatorStateController just need the public function UpdateWalkAnimationState() and a private backing bool:

      public class AnimatorStateController : MonoBehaviour
    {
         [HideInInspector] bool walkAnimationStateOn = false;

public void UpdateWalkAnimationState(bool running)
        {
            walkAnimationStateOn = running;
        }
}

2023 the bug still exist wow

1 Like