How do I properly use Coroutines, yield and animators to sequence commands ?

I’m having a hard time understanding how yield works inside coroutines.

I’m using a coroutine to start a series of animators in a certain sequence.

When I use waitForSeconds in between commands, this works as I would expect… however, when I want to wait when an animator has finished, and then execute the next command, it does not work.

In my example below, the last command (loadScene2) should only be fired after the animator (scene1-ufos) has finished, but instead it is fired at the same time as my last command.

What am I doing wrong here ?

    IEnumerator startMikeAnimation()
        yield return new WaitForSeconds(5);
        yield return new WaitForSeconds(5);

        while (animatorUfos.GetCurrentAnimatorStateInfo(0).normalizedTime < 1.0f)
            yield return null;


yield return null; - returns nothing - and I’m pretty sure it just breaks the while loop

yield return new WaitForSeconds(animatorUfos.GetCurrentAnimatorStateInfo(0).length - 0.01f);

should do the trick - I subtract the 0.01f - since I was using it for a short not really looping anim and would sometimes see it loop back to the first frame(object pooled explosion) - your mileage may vary

Many thanks for that snippet… this looks like a cleaner way to achieve what I am trying, but sadly, it does not work in my case…

It seems that when I play a “state” called “scene1-ufos” from my Animator animatorUfos, that this clip does not reflect my currentAnimatorState… Is that possible ?

I think that my empty “New State” is always seen as the currentState, but I’m not sure.

There’s no way to debug.log the currentstate name, and I can’t find a way to programmatically set the current state.

My animator is set up like this, I have added an empty new state to make sure this animation does not start automatically. I think I’m doing this wrong.

This is all awfully complicated for the simple 2D animations I’m trying to trigger.