Why switching Animator State is taking 2 frames?

I am creating a coroutine where I am detecting if the animator state is finished. And I Don’t understand why I need to wait for 2 frames to correctly detect current AnimatorStateInfo.

If you want to try it yourself delete both yield return null statements and run Start ScaleUp from context menu, and without them it will fail to detect correct AnimatorStateInfo
(try to run it at least 2 times in row)

public class AnimManager: MonoBehaviour
{

public enum AnimTypes
    {
        scaleUp,
        scaleDown,
    }

public Animator TestingAnimator;


    public IEnumerator PlayAnimatorWaitToFinish(Animator myAnimator, AnimTypes myAnimType)
    {
        myAnimator.Play(myAnimType.ToString(), 0, 0);

//HERE I NEED TO WAIT 2 FRAMES
        yield return null;
        yield return null;

        while (myAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1)
        {
            yield return null;
        }
    }

    [ContextMenu("Start ScaleUp")]
    public void StartUp()
    {
        StartCoroutine(StartAnimatorRoutine(TestingAnimator, AnimTypes.scaleUp));
    }

    public IEnumerator StartAnimatorRoutine(Animator myAnimator, AnimTypes animType)
    {
        yield return PlayAnimatorWaitToFinish(myAnimator, animType);
        Finish(myAnimator);
    }

    public void Finish(Animator myAnimator)
    {
        Debug.Log("Finish Animator "  + myAnimator.gameObject.name);
    }
}

my Animator controller screen capture is in attachement

That’s interesting. I find it really stupid that you have to wait even one frame, but I’ve never seen it need to wait two.

Maybe it has something to do with when context menu functions are executed so it might work properly if you run it when a key is pressed during Update.

You might also be interested in Animancer (link in my signature) which lets you avoid Animator Controllers and just play whatever AnimationClips you want on demand. It doesn’t force you to wait a frame to check or modify the current state and it has a few inbuilt ways of waiting for animations to end.

Thank you @Kybernetik i will check it, but it looks pretty cool. But still i would like to know why waiting 2 frames is needed. I will try to chceck the context menu if it is using one frame. But still it is weird to wait one or two frames for sure