Photo from pexels.com by Mike

Is the coroutine done?

Coroutines and Callbacks in Unity

How to have a coroutine ring a bell when it’s done

Geek Culture
Published in
3 min readJun 28, 2022

--

When a Coroutine is started like this in Unity:

StartCoroutine(CoroutineName(vars));
NextCommand();

a piece of code runs outside the Update pattern. This means that the execution of that code is not restricted to the frame from which it started, but can span across several frames.

Usually, this is obtained using the yield return command, which forces the program to “yield” the control until a condition is met (a time has passed or some bool turned true…).

Once the coroutine is started, the program continues with the successive commands, leaving the coroutine to its own life cycle.

So, a question arises quite naturally:

How can I tell if the coroutine has finished?

An Ugly Way — Object Probe

We might provide a null object to the coroutine:

IEnumerator MyCoroutine(MyClass probe){
DoStuff();
yield return _waitForSometime:
DoOtherStuff();
probe = new MyClass();
}

and, at the end of the coroutine, we initialize it.

--

--

Daniele Quero, PhD
Geek Culture

A professional developer with passion for game developing and skill-growing. A former Nuclear Physics Researcher who changed his life to pursue his dreams