Skip to content

Published: 11/1/2026

Your First Game in Unity: Ghost Evader: Part 10: Handling the Player’s Death

In this post we are going to focus on handling the player’s death.

Welcome to the next step in the setup for our game, Ghost Evader. This is a simple 2D top-down game where the player must collect coins and stay alive as long as possible. In this series we are going to go step by step and explain everything as clearly as we can.

We have a lot to cover so let’s get started!

Starting and Stoping the Enemy Spawner

Whenever a round begins we want to start the enemy spawner. When the player dies, the rounds stops we want to stop the spawner, possibly give the player a countdown then start the next round.

We already wrote the start spawner and the stop spawner functions, we just need to call them. First we need a reference to the ObjectSpawner class on the EnemySpawner game object:

[Header("Spawners")]
[SerializeField] private ObjectSpawner enemySpawnerScript;

If you have been follwing the series up to this point this should be straightforward. We have covered all of the aspects of the [Header], [SerializeField] C# attributes.

We will need to change the signature on the ObjectSpawner script’s StartSpawner() and StopSpawner() from private to public:

public void StartSpawner()
    {
        if (_spawningCoroutine != null)
        {
            StopCoroutine(_spawningCoroutine);
        }

        _isRunning = true;
        _spawningCoroutine = StartCoroutine(SpawnObjects());
    }

This will allow us to call StartSpawner() from the GameManager script and when the Player dies we can call StopSpawner()

private void StartRound()
{
    _playerScript = Instantiate(playerPrefab, Vector2.zero, Quaternion.identity);
    enemySpawnerScript.StartSpawner();
}

public void PlayerHasDied()
    {
        enemySpawnerScript.StopSpawner();
        Debug.Log("Player Has Died");
    }

Last, we can now remove the Start() function from the ObjectSpawner class entirely. We have exactly the same functionality as before. The diference is now the ObjectSpawner class will stop spawning enemies to die as soon as they are spawned!

Save the code, got to the editor. Don’t forget to attach the EnemySpawner game object to the GameManager and test!.

Adding A Timer to the Next Round

Since we added a structure for multiple rounds let’s add support for it!

First lets add a variable to indicate how much time there is between rounds. We’re also going to use this to set the number of lives in a way we can define it in the editor.

 [Header("Game")]
 [SerializeField] private int numberOfLives = 3;
 [SerializeField] private float timeBetweenLives = 3.0f;

We’ll also add a Unity Coroutine to time this rather than update. So let’s also add the using statement:

using System.Collections;

Next let’s write the countdown:

private IEnumerator DelayedStartRound()
{
    yield return new WaitForSeconds(timeBetweenLives);
    StartRound();
}

In this case we do not need to create a variable to hold for the delay since we aren’t running this inside a tight loop.

Next we will add the data needed for when the game starts:

private void StartGame()
{
    // Add any startup code
    CurrentScore = 0;                     // Set this explicitly
    NumberOfLives = numberOfLives;        // Set this to what we have in the Editor
    StartRound();
}

For now, lets print out the number of lives the player has to the console

 private void StartRound()
{
    _playerScript = Instantiate(playerPrefab, Vector2.zero, Quaternion.identity);
    enemySpawnerScript.StartSpawner();
    Debug.Log($"Player has {NumberOfLives} lives")
}

You may not have seen a $ sign right before the string before. I am using what is called string interpolation. This allows me to pipe in the variable or in place edit the string.

Last, lets upate the player:

public void PlayerHasDied()
{
    Debug.Log("Player Has Died");
    enemySpawnerScript.StopSpawner();

    if (numberOfLives > 0)
    {
        StartCoroutine(DelayedStartRound());
    }
    else
    {
        Debug.Log($"Player has no more lives left !!! Game Over!!!")
    }
}

If you find this content useful please consider supporting me.

☕ Buy Me a Coffee