My 90 day journey becoming a Unity game developer: Day-8 & 9

Rhett Haynes
4 min readMay 31, 2021

--

Objective: Creating a spawn manager to spawn enemies at random positions along the X-axis using a coroutine to handle the time between an enemy spawning. Also, we will create an enemy container that will be the parent of the enemies spawned which will allow us to manipulate them in the future. The final result will look like this:

Enemies spawning every 5 seconds from the Spawn Manager.

Create an empty game object name Spawn_Manager. Create a C# script with the name SpawnManager, and drag it onto the game object Spawn_Manager.

Inside the SpawnManager script, let’s create a variable that will instantiate an enemy through our Spawn_Manager game object. Now a decision was made to use a coroutine instead of using the while() function to instantiate our enemies. Using the while() function would allow us to instantiate our enemies randomly, but the condition to do it can have us trapped in an infinite loop. This loop’s condition which would be true constantly will cause our memory or system to crash. You wouldn’t be able to break out of the loop, therefore enemies will be spawned repeatedly until there is an overload.

Instead, we will create a method using the IEnumerator function name SpawnRoutine(). With this coroutine we will be able to pause the time it takes to spawn another enemy. This will allow your system a chance to breath and give your code inside the coroutine a chance to get out if the condition is false. We will use the while() function inside of the coroutine as this is an ideal situation to use it.

Inside the while(true) function, we will instantiate the enemy prefab with no rotations. Before we do that, we will create a Vector3 variable where the enemy spawns at random positions along the X-axis at the top off screen. Then we will instantiate our enemies at that new variable’s position, and make the enemies spawn every 5 seconds.

Coroutine to spawn enemies every 5 seconds randomly on the X-axis.

We will call on this coroutine inside the Start() function like so:

There is 2 ways to use StartCoroutine, which is with the parameter passed SpawnRoutine() or as a string “SpawnRoutine.” Using the string method name you can use the StopCoroutine function with a specific method name. The negative doing this way is that the string version has a higher runtime overhead to start the coroutine and you can only pass one parameter. In most cases you want to use the StartCoroutine() variation.

StartCoroutine using SpawnRoutine method inside of the Start function.

Click on the Spawn_Manager game object, go to the script component in the Inspector, and drag the Enemy onto the enemy prefab slot. Then run your game to see the enemies being spawn from random positions.

Enemy prefab added to variable enemyPrefab in Spawn Manager script.
Enemies spawning every 5 seconds.

Now if we noticed in our Hierarchy that it was being cluttered with all of the spawned enemies which can make it difficult to debug or run certain tests on. We’re going to create a container that will keep our enemies spawning within it, and make it a child of the SpawnManager game object.

Inside the SpawnManager script, create a game object variable called enemyContainer and serialize for the inspector. Save it and head over to Spawn Manager game object. Drag the Enemy Container game object to the enemy container variable slot.

To have a reference to the instantiated enemies to manipulate, we will instantiate the enemy within a new variable inside the SpawnRoutine() method.

To manipulate the enemies, we need to choose a parent for these enemies which will be the EnemyContainer.

This is the final result of our SpawnManager’s script code.

Next time we will work on stopping the enemies from spawning when the player dies. Until then …

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet