Unity Basics: Coroutines

How the Coroutines can help manage object timing in Unity

Amber Chunn
3 min readJul 20, 2022
While clock with no numbers on a bright orange background
Photo by stefan moertl on Unsplash

Coroutines sound scary, or perhaps fancy, depending on your perspective. However, in reality, they are a nifty tool we can leverage for common events in our games. Unity’s Coroutine method allows you to spread task(s) over several frames¹. More practically, you can think of a Coroutine as a function that is executed in intervals². Coroutines are most useful when dealing with objects that need prolonged effects, or when the modification of an object’s properties is needed over a period of time, instead of the standard single frame. “How do you use this magic?”, you exclaim. I’m glad you asked.

Before we dive into code, here are the main points you need to know when working with coroutines in Unity. First, coroutines make use of C#’s IENumerator() interface, and to be valid, they must contain at least one yield statement. Second, as per normal functions, you will need to define the coroutine function and then invoke the function in order to actually make use of it. One final thing to note is that unlike general programming norms, you will sometimes want to use an infinite loop. “What!?” Yep, infinite loops are often used inside coroutines for different effects that run over the entire time of the script. The important thing to remember is to provide some sort of delay between spawns so that the hardware your game is running on does not run out of resources, as this will crash your game. If done correctly, this will help make your code more efficient, as you won’t need to bombard the Update() method with extra function calls on each frame.

In our example, we would like to spawn enemies while our game is running. We want this to happen in constant intervals for the entire life of our player. When our player dies, the spawning variable will change to false and the perpetual stream of enemies will end. We will be using a spawn manager script to house our code for our spawn routine, make sense? Good!

Step 1: Set Up Boolean Spawn Variable
Inside our spawn manager file, we will add a private variable to track wether the spawning mechanism is active or not.

Step 2: Create Coroutine Function
For our function, we want to create a (mostly) infinite loop that continuously spawns enemies until our player dies. To do this, we set up a while loop that checks our _enemySpawn variable as the condition. Remember, the only thing responsible for stoping this loop is this variable. Once we set up our loop, we can add our spawn functionality. We simply instantiate a new enemy prefab at a random horizontal point, with the vertical point set to the top of the screen. Lastly, we add the cooldown time period.

Step 3: Invoke coroutine
Now all that’s left is to kick off our newly minted coroutine! We will do this as soon as our script starts.

Full Spawn Routine Code

Voilà! We now have enemies spawning until our player expires.

Happy Coding!

Reference:
1. https://docs.unity3d.com/Manual/Coroutines.html
2. https://learn.unity.com/tutorial/coroutines

--

--