Spawning enemies with Coroutines in Unity
Now we have a setup allowing us to shoot some enemies, let’s make it more interesting by allowing them to spawn regularly. A common way to do this is by using a Coroutine in a Spawn Manager script.
A Coroutine is a special kind of method in C# (of type IEnumerator) that allows us to ‘pause’ the running of code by calling the ‘yield’ command. There are several ways to use the yield command to specify different conditions that will trigger the code resume again, for example —
To be able to use this in our game, we’ll need to create a new script named ‘SpawnManager’ and open it up. If we consider what we want to achieve, we could begin with some pseudo code like this —
From this, we can determine straight away, we will need a reference to our Enemy prefab — which we can serialize and assign in the inspector. With that done, we can look into our Coroutine. As I mentioned, it is a method of type IEnumerator —
As you can see from this screenshot, it is showing an error with our method declaration. This is because we have declared a return type function. Just as an int return type function requires that you return an int value, an IEnumerator requires that we return a yield (or delay) inside the Coroutine.
As shown in the examples above, we have the ability to ‘WaitForSeconds’ to delay the code for the specified length of time. This requires a value of type float in seconds, so to wait for the 2 seconds as stated in our pseudo code —
While here I have used the value ‘2f’ for our delay (2 seconds), we could also use a float variable here and adjust it in the inspector if desired — we could even use a random number in there to give a randomness to the time between enemy spawns. I have also created a random spawn position for our enemies each time they spawn. I covered using the Instantiate method in this previous article.
With that taken care of, it is all wrapped up in a cosy while loop that will continuously run until our switch bool becomes false and then the Coroutine will stop running.
The last thing we need to do is start running the Coroutine. Although this is a return type method, we cannot call it like a regular method. We actually need to ‘start’ the Coroutine, this can be done easily with the appropriately named ‘StartCoroutine’ method.
When we call ‘StartCoroutine’, we need to specify which coroutine we want to start and be sure to initialize it with ().
NOTE: More information on Coroutines and the different ways we can invoke them can be found here.
This is all the code we need, just save the script, head back to Unity and create a new empty GameObject named ‘Spawn_Manager’ — or anything you like really! With that created, attach our new script to the new GameObject and assign our Enemy prefab to our script.
I had the ‘Y’ spawn co-ordinate deliberately low so the spawns can be seen as they happen, simply adjust that spawnPos variable to whatever value works for your situation. I’ll leave you to look into destroying the laser after killing an enemy…