Today we will be creating our spawn manager system to continually spawn enemies on the screen until the player is dead.
We will start by creating an empty game object in the hierarchy and naming it Spawn_Manager. Then create a new C# script named SpawnManager, then attach the script to the Spawn_Manager game object. If you still have an Enemy game object in the hierarchy now you can delete it, as it’s not needed any longer. We will also select the Spawn_Manager and create another empty game object and name it Enemy Container. This is where all the newly spawned enemies will be contained so they aren’t spread all over the hierarchy.
Open up the SpawnManager script and let’s begin. To create the desired effect of creating a timer where after 5 seconds another enemy will spawn, we will be using a Coroutine. To start a new coroutine we will create a new function with the prefix of IEnumerator. This tells Unity that we are creating a coroutine and making a timer. One thing our new function will need to work is a line of
This will tell the script to wait for 5 seconds before running the function again. Coroutines are where major issues can occur with memory dumps and game crashing. Most often it’s because there is an infinite loop that can’t get out which will cause the game to crash and lock up a computer.
That was a little back story and we will now start from the top. First, we will need some variables. We will want two private GameObjects, one called _enemy and the other _enemyContainer. SerializeField both of those. The final variable we will need is a private bool called _stopSpawning and have it equal to false to begin.
Now we create the coroutine and start it as IEnumerator and name it SpawnRoutine. Next, we will create a while loop and while _stopSpawning is equal to false, the following will happen. Along the top border and the left and right borders, an enemy will be instantiated at a random location along the x-axis. We will also make these enemy clones spawn in as a child of the enemyContainer. After those lines are ran, we will then begin our counter, our WaitForSeconds.
As is, this will run continually even after the player dies. In our case, we want to stop enemies from spawning when the player dies. So we’re going to create another function called OnPlayerDeath. This function needs to be public as we will be accessing it from the ‘Player’ script to tell if the player is dead or alive. Inside the OnPlayerDeath function, we will set _stopSpawning to true. At the moment it will show you an error, we will fix that when we get in the ‘Player’ script.
In the ‘Player’ script we need to create a new variable to reference the Spawn Manager. It will look like this.
In our Start function, we need to find the SpawnManager. To do that we will set _spawnManger equal to GameObject.Find. Here we will search for the exact spelling of the Spawn_Manager in our hierarchy. We will then get the SpawnManager component and initialize it.
We should probably create a null check on the _spawnManager now to make sure we aren’t going to have major issues. All together in the Start function, it will look like this.
The final thing we need to add is to the Damage function on the ‘Player’ script. Just before destroying the gameObject we will call the OnPlayerDeath function from the _spawnManger.
Now our finished product has enemies spawning every 5 seconds, but when the player dies, only the enemies that remained will stay on the screen. No more will spawn.
That’s it for the enemy spawner