My 90 day journey becoming a Unity game developer: Day-10
Objective: Stop spawning new enemies when the player dies. This is the final result with the same enemies on the screen when the player dies.
To begin, inside the SpawnManager script we will create a global variable with a boolean value of false. Then, we will go inside the coroutine SpawnRoutine(), and change the while condition from TRUE to the stopSpawning variable which will still allow the enemies to be spawned.
Next, we need the player to communicate with the SpawnManager script to stop spawning. Since it’s not good practice to directly modify the value of a variable, let’s create a method to control the variable we want to modify. The method’s name will be OnPlayerDeath(), and the variable stopSpawning will be changed to TRUE.
Head over to the Player script, create a global variable with type SpawnManager and name spawnManager. Inside of the Start() function, use the spawnManager variable to communicate with the Spawn Manager game object. This will allow us to get the Spawn Manager’s component. Add a NULL check to see if the spawnManager is active. We will do it inside of the Start() function because this is the best place to check for NULLS.
Finally, go to the Damage() method, and under the if (lives) condition we will use our OnPlayerDeath method to stop new enemies from spawning when we die.