Day 9 — Coroutines in Unity
Mom said it was my turn to play!
For every frame of action in a video game (usually 60 frames per second), each function in your code runs until completion before going to the next function that needs to be done. If you accidentally create an infinite loop in one of the functions, your game could crash or appear to freeze. When that happens, the player will probably become unhappy.
In the Space Shooter 2D game, I needed to create a way for the game to spawn a new enemy every 4 to 5 seconds. I decided to create a simple, but continuous loop, that waits the specified amount of time and instantiates a new copy of the enemy game object at a random position just off the top of the play area. There are multiple ways to do this, but a coroutine works great in this situation and takes away the worry of having the program get stuck in that loop.
A coroutine is like a normal function but has the ability to pause execution and had control back to Unity but then continue where it left off on the next frame.
Here is the code for the SpawnManager. It is attached to an empty game object placed in the game scene. This way the script is active because the object is active, but it is not seen and does not affect the other game objects unless coded into the script.
In lines 7 and 8, I initialized the two variables needed; a spawn rate (in seconds) and a GameObject variable to hold the enemy game objects. As always, make sure to reference the enemy ship from the Prefabs folder and not the hierarchy.
The Start() function is called once as soon as the object the script is attached too is loaded in and ready. On line 13, the code call starts the coroutine called SpawnRoutine(). It only needs to be called the one time.
The While() statement on line 24 check to see if true is true; it is (and always will be, otherwise something is very very wrong). This creates an intentional infinite loop. But, because this is a coroutine, we can use the yield statement to tell Unity to go do everything else it needs to do and come back.
The WaitForSeconds() statement tells the code within the While loop to hold off on repeating until the specified amount of time has passed, in this case 5 seconds. Unity keeps stepping through all of the other active scripts in the game, checking in with this coroutine once each frame. After the allotted time has passed, the coroutine spawns a new clone of the enemy and starts the wait all over again.
I disabled the player game object and set the spawnRate to 1 second, to show how this looks at run time. Because I used the coroutine to handle the time delay, the game play is still nice and smooth.
For a more detailed explanation and examples, check out the official Unity documentation for coroutines here: https://docs.unity3d.com/Manual/Coroutines.html
What’s next? Well I think I’ll work on cleaning up the Hierarchy window. All of those cloned enemy game objects are making me nervous. 😬 So tomorrow’s article will deal with spawning objects and managing the clutter.
See you there!