Unity guide

Using coroutines in Unity

A quick guide to use coroutines in Unity

Fernando Alcantara Santana
4 min readMay 12, 2021

Objective: Use a coroutine to spawn an infinite number of enemies in a space shooter game

In order to spawn enemies inside our Unity scene we can use coroutines, as they’re a way to handle events in a certain amount of time and not just in a single frame like in a normal function.

The principal benefit of using a coroutine to spawn enemies into a game in Unity is to let the computer execute a function every time we choose to, so that it doesn’t have to process a condition to do it every frame inside the update function.

To begin, we need to have a prefab of our enemy object in order to produce different clones inside our scene:

In order to handle the coroutines to spawn the enemies, we need a spawn manager, which can be an empty gameobject inside our scene:

Then, we need to create the script for our spawn manager and add it as a component inside our empty gameobject in the scene:

Once inside the SpawnManager script we need to create a variable to represent the enemy prefab. Make the variable private and use [SerializeField] above it so that it’s possible to drag the prefab into the Unity inspector.

This is the new variable in the inspector where we need to drag the enemy prefab
Once you drag the prefab it will look like this

Next, inside the SpawnManager script create an IEnumerator function, which will allow us to use the coroutines to create the enemies:

Now we need to have a while infinite loop, so that the coroutine keeps processing what is inside and the enemies spawn infinitely when we play:

After that, we need to randomize the x position of every enemy, so we create a new Vector3 with a Random.Range with the x limits of our game space, the y axis position where they are going to start spawning and a 0 for the z axis:

Note: I used xPosToSpawn to name the Vector3 as it’s the most important coordinate to decide the position of the enemy instance

Next, we need to instantiate the prefab of our enemy using the Instantiate function with a reference to the previous created variable (_enemy), the position where the instance will appear (xPosToSpawn) and the default rotation (Quaternion.identity).

Now we need to set the time for our coroutine to process the code above again by using the yield keyword, which indicates that this loop is an iterator block. Then add the value to be returned, which will be the WaitForSeconds function, that ensures the time to wait before running the code above again:

This way we’ll spawn an enemy gameobject every 5 seconds

Finally, to execute the coroutine we need to use the special function call StartCoroutine referencing the coroutine to be executed. Do this inside the Start function to call it once as we have already an infinite loop inside the SpawnEnemies function.

Now we can run the game in Unity and see that an enemy is spawned every 5 seconds:

And that’s how we can use coroutines in Unity to spawn infinite enemies in our game! I’ll see you in the next post where we will check how to spawn gameobjects in Unity without clutter in the hierarchy.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).