Nerd For Tech
Published in

Nerd For Tech

Spawning with Coroutines

Now that we have an enemy built into the game with hit detection, it’s time to start spawning them. The concept of a spawn manager is simple, as we just want it to continuously spawn enemies as the game is running. As to how we get around to that, we can easily search through google for a type of spawning sequence that repeats itself over a period of time. Before we do that though, we have to create an object to represent our spawn manager and a script for it.

Creating spawn manager

As we can see, all we simply have to do is create an empty object. This object will not show up in our live game, but serves as just a place holder for our script.


In order for us to delay an event in our game for a period of time and then restart it again after that period of time has been reached, we have to use something called coroutines. They allow us to create sequenced events which then let us yield events.
First, we have to figure out how the scripting API works for coroutines so we know how to build the code correctly within our script.

As we can see from the script API above, with a coroutine, we need to be sure to have the “yield” within the script. This allows us to perform our task multiple times and in intervals that we desire.

Starting our coroutine

As seen from above, we can start our coroutine by creating an IEnumerator SpawnRoutine(). This line of code will create a new system for us to use in our future code. From there, we type while (true). It is important that with this specific line of code, we place some sort of limiter in it’s place. In our case, we will be spawning something new every 5 seconds. If we had no limiter in place, we can crash not just the program, but our computer because we would have created an infinite loop that will run at all times. This infinite loop will overload everything and cause crashes.

Also, we need to make a GameObject for our script to pull information from. So, we will create a private gameobject “_enemyPrefab”, save it and go into Unity and drag over our enemy prefab we just created to this section:

Applying enemy prefab

From here, we can now add to our while line of code and let it know that we want to instantiate our enemies.

With this line of code placed within the while section, we are telling Unity that we want our Enemy to spawn at an interval of every 5 seconds.
Finally, in order to get our enemy unit to spawn, we need to let Unity know we want to start our coroutine, so what we will do is under Void Start, we enter StartCoroutine(SpawnRoutine()). Once we have done this, we can go back into our Unity editor and check to see if our units spawn on a 5 second interval.

Spawning enemies

Now that we have our spawning system in place, we will look into making it a little bit cleaner and stopping our spawn manager when our player dies in our next article.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store