Creating a Retro Game Over Behavior

Christian Carter
4 min readJun 2, 2021

--

After adding the UI for the lives it feels like the right time to add the game over behavior. To start off you need to add another text object to the Canvas. Change the name to “Game_Over_Text” and update the text to show “GAME OVER”. Set the color to white. Change the font size to 50. This will probably make the text disappear. That is because of what the Horizontal and Vertical Overflow are set to. Change them both to Overflow. Now increase the box around the text to fit the entire text. Last, center the text in the scene.

The next step is to code the script so that the game over text is displayed when the player has zero lives. First in the UIManager script create a variable to reference the game over text object.

[SerializeField] private Text _gameOverText;

Make sure to connect the game over text object to the variable in the Unity editor. In the start function turn the game over text object off so that it is not displayed

_gameOverText.gameObject.SetActive(false);

The last thing to do is when the player has 0 lives to turn the game over text object back on. The UIManager already has a function that receives the current number of lives the player has so this is a great place to add the game over text. Just remember to check if the current lives is 0.

if (currentLives == 0)
{
_gameOverText.gameObject.SetActive(true);
}

To get the text to flicker in the game over state you need to add a coroutine. The coroutine will loop turning the game over text on, wait, turn it off, and wait again then loop back to the start.

IEnumerator GameOverFlickerRoutine()
{
while(true)
{
_gameOverText.gameObject.SetActive(true);
yield return new WaitForSeconds(0.5f);
_gameOverText.gameObject.SetActive(false);
yield return new WaitForSeconds(0.5f);
}
}

Make sure you start the coroutine.

StartCoroutine(GameOverFlickerRoutine());

The last thing to do is make it so you can restart the game from the game over screen. First, add instructions for the button or key to push to restart the game. This can be included just like adding the game over text above. Don’t make the text as large as the game over text. Set the text to “Press the ‘R’ key to restart the level”. You can put this text more towards the bottom of the scene. The code to activate it can be just like the code for the game over text though you don’t need it to flash.

Last, you need to implement the code so that when you press the key, it does restart. You should create an empty Game_Manager object and attach a GameManager script to it.

To be able to reset the scene you need to include unity SceneManagement. You can do this by adding using UnityEngine.SceneManagement; to the top of the script. Add a variable for if the game is over. Add a function that can be used to set the game over variable to true.

private bool _isGameOver;public void GameOver()
{
_isGameOver = true;
}

In the update function add code to check if the ‘R’ key was pressed. It also needs to check if the game is over. Then use SceneManager to load the scene.

if (Input.GetKeyDown(KeyCode.R) && _isGameOver == true)
{
SceneManager.LoadScene(0); //Current Game Scene
}

If you have not already done so you will need to add the scene to your build settings. This can be done by going to File->Build Settings. Then click “Add Open Scenes” and then close the build settings. The last thing to do is to call the GameOver function from the UIManager. First create a GameManager variable then in the start function assign the game manager to it.

_gameManager =   
GameObject.Find("Game_Manager").GetComponent<GameManager>();

Then during the game over sequence call _gameManager.GameOver();. Now the game will display game over and you can restart by pressing R.

Good Luck Adventurer!

-Chris

--

--