Search Unity

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

Discussion in 'Animation' started by sleep, Jan 25, 2014.

  1. sleep

    sleep

    Joined:
    Aug 7, 2013
    Posts:
    8
    ...unless I explicitly update the Animator twice:

    Code (csharp):
    1.  
    2. Animator m_anim = ...;
    3. m_anim.Play("GenericSpinReveal");
    4. m_anim.Update(Time.smoothDeltaTime);
    5. m_anim.Update(Time.smoothDeltaTime);
    6. // Assert test passes only if I have the two  updates above.
    7. DebugUtils.Assert(m_anim.GetCurrentAnimatorStateInfo(0).IsName("Base.GenericSpinReveal"));      
    8.  
    Any way around this? Needless to say this breaks a lot of basic logic you might want to do. :eek:

    $Screen Shot 2014-01-25 at 10.14.00 PM.png
     
  2. sleep

    sleep

    Joined:
    Aug 7, 2013
    Posts:
    8
    Nobody?
     
  3. leandro_nw

    leandro_nw

    Joined:
    Jul 17, 2012
    Posts:
    15
    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.
     
  4. crafTDev

    crafTDev

    Joined:
    Nov 5, 2008
    Posts:
    1,820
    This is currently a very weird issue for me.

    What was the solution?

    Edit: Besides using Update...
    Thanks,
    jrDev
     
    Last edited: Jul 1, 2018
  5. Mock

    Mock

    Joined:
    Aug 25, 2014
    Posts:
    1
    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
    Code (CSharp):
    1. animator.SetTrigger('hide');
    2. while(animator.GetCurrentAnimatorStateInfo(0).IsName("Hiding")){
    3.   yield return null;
    4. }
    But I have switched it to
    Code (CSharp):
    1. animator.SetTrigger('hide');
    2. while(!animator.GetCurrentAnimatorStateInfo(0).IsName("Hidden")){
    3.   yield return null;
    4. }
    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.
     
  6. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    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.
     
    dithyrambs, comiljou and SirDaniel274 like this.
  7. dithyrambs

    dithyrambs

    Joined:
    Mar 27, 2018
    Posts:
    5
    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

    animator.Play(animName, 0, 0f);
    //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?
     
  8. Taalon

    Taalon

    Joined:
    Mar 18, 2022
    Posts:
    2
    Still broken. Awesome work, Unity.
     
  9. Davex6

    Davex6

    Joined:
    Mar 29, 2023
    Posts:
    49
    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:

    Code (CSharp):
    1. public class WalkBehaviour : StateMachineBehaviour
    2. {
    3.     AnimatorStateController aniController;
    4.  
    5.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    6.     {
    7.         if (aniController == null)
    8.             aniController = animator.gameObject.GetComponentInChildren<AnimatorStateController>();
    9.         aniController.UpdateWalkAnimationState(true);
    10.  
    11.     }
    12.  
    13.        override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    14.     {
    15.         aniController.UpdateWalkAnimationState(false);
    16.  
    17.     }
    Then in the AnimatorStateController just need the public function UpdateWalkAnimationState() and a private backing bool:
    Code (CSharp):
    1.       public class AnimatorStateController : MonoBehaviour
    2.     {
    3.          [HideInInspector] bool walkAnimationStateOn = false;
    4.  
    5. public void UpdateWalkAnimationState(bool running)
    6.         {
    7.             walkAnimationStateOn = running;
    8.         }
    9. }
     
  10. smartplay

    smartplay

    Joined:
    Feb 13, 2019
    Posts:
    21
    2023 the bug still exist wow