Unity: Chaining Coroutines

June 29, 2025

Sometimes, you want a coroutine to wait for another to finish before it starts, the easy way to do this is to simply yield on the IEnumerator.

Waiting for a coroutine in another coroutine

public IEnumerator CoroutineFoo() {
    yield return CoroutineBar();

    //Do stuff
}

However, what if you want to have multiple coroutines execute one after another?

We can use a simple queue system to achieve this, enqueue the coroutines you want to run in order, then flush the queue. The queue flush should also be a coroutine, in order to yield on the queued coroutines.

Coroutine Queue

public void QueueAnimationCoroutine(IEnumerator enumerator)
{
    actionQueue.Enqueue(enumerator);
}

public IEnumerator CoroutineFlushQueue(Action onComplete)
{
    while (actionQueue.Count > 0)
    {
        var enumerator = actionQueue.Dequeue();
        StartCoroutine(enumerator);

        yield return enumerator;
    }

    onComplete.Invoke();
}

That's all I got for now.