Stop Spawn Routines Upon Player Death in Unity3D

Pete Thomasberger
4 min readMay 15, 2021

--

Objective: Damage the Player GameObject 3 times before being destroyed. When the Player GameObject is destroyed, stop all GameObject spawning.

Step 1: Create a Damage Method

In the Player script, using [SerializeField], create a private variable of type int called _lives with a value of 3. This variable value represents how many times the Player GameObject can collide with the Enemy GameObject before being destroyed.

Create a new public method called Damage() which will be called every time the Player and Enemy GameObjects collide.

Each collision will reduce the Player’s number of lives by 1, so we will reduce the _lives variable by 1 every time the Player and Enemy GameObjects collide.

Once the Player GameObject has taken damage 3 times, the Player will be destroyed. Use an If statement inside the Damage() method stating, if the value of the _lives variable is less than 1, destroy the Player GameObject.

Step 2: Call the Player’s Damage Method

Inside the Enemy script, within the OnTriggerEnter2D() method’s If statement where other.tag equals the Player, use GetComponent to access the Player script’s Damage() method.

Then, if the Player script is not equal to null, call the Damage() method. This will reduce the Player script’s _lives variable by 1 every time the Player and Enemy GameObjects collide until the _lives variable is less than 1.

Step 3: Stop Spawn Routines on Player Death

Inside the SpawnManager script, create a private variable of type bool called _stopSpawning that is set to false.

Create a new public method called OnPlayerDeath(), and within this method, set the _stopSpawning variable to true.

Next, within the EnemySpawnRoutine() and PowerUpSpawnRoutine() methods, replace the true arguments in the while statements with _stopSpawning is == false. This allows for the spawn routines to instantiate GameObjects only while the _stopSpawning variable is set to false. Once the _stopSpawning variable becomes true, the spawn routines will stop instantiating GameObjects.

Step 4: Call the OnPlayerDeath Method

Inside the Player script, create a private variable of type SpawnManager called _spawnManager.

Inside the Start() method, find the Spawn_Manager GameObject and use GetComponent to access the SpawnManager script and set this GetComponent statement as the value of the _spawnManager variable.

It is also best practice to error check this type of statement in the instance that the GameObject is not found. This can be done with an error log notification printed to the Unity console stating “The Spawn Manager is NULL” in the instance where the _spawnManager variable is equal to null.

Now that the SpawnManager script can be accessed through the _spawnManager variable, call the OnPlayerDeath() method within the If statement inside the Player script’s Damage() method. This states that if the _lives variable value is less than 1, the OnPlayerDeath() method will be called.

Save the script, go back into Unity, play the scene, and after the Player and Enemy GameObjects collide 3 times, the Player GameObject will be destroyed, and the spawn routines will stop instantiating GameObjects.

--

--