Unity Game Over Screen

In the last lesson, we outputted a message to the Console window when the game was over. In this lesson, we'll replace the debug code with an actually Game Over screen.

 

We'll add just a simple black screen when the number of lives the player has gets to zero. In reality, you'd want to display a menu of some sort where the player can have another try. But we haven't done menus yet, so we'll go with a black screen.

Right-click on your Background item in the Hierarchy. From the menu, select UI > Panel. Rename your new panel to GameOver. In the Inspector, change the color to black. (Make sure the Alpha slider at the bottom of the color dialog box is up to full.)

Right-click your new GameOver panel and select UI > Text - TextMesh Pro. Rename the new TextMesh Pro item GameOver-Text.

As the text for your new text item, delete the default New Text and type Game Over. You can adjust the font size and font to anything you like. If you make your font size too big, however, it will wrap on screen. To prevent this, select Disabled from the Wrapping dropdown box:

Font settings for a game over panel

Make sure you have the Game tab selected at the top and not the Scene view. The Game tab should look like this:

A game over screen

We need to disable the GameOver item. So, click back on your GameOver panel in the Hierarchy. In the Inspector on the right, uncheck the box at the top:

Deselecting the game over screen in the Unity Inspector

Now go back to your PlayerHealth script. Add this variable to the top of your code with all the others:

public GameObject gameOverScreen;

And make sure you have this using statement at the very top:

using System.Collections;

Now add this IEnumerator at the bottom of your code, but inside the final curly bracket of the class: (It can go just after theupdateHealthText method):

IEnumerator DisplayGameOver()
{

yield return new WaitForSeconds(0.25f);
gameOverScreen.SetActive(true);

}

We're waiting for a quarter of a second until we display the Game Over Screen. The next line uses the inbuilt method SetActive to switch the Game Over panel back on (true).

Now locate this comment we added:

//DISPLAY GAME OVER SCREEN

In its place, add this line: (The line in bold.)

if (numOfLives == 0) {

gameOver = true;
StartCoroutine(DisplayGameOver());

}

Your code should look like this in your editor (new lines are highlighted and we've contracted the Start and Update methods):

Unity C# code for a game over screen

Save your work and go back to Unity. With the First person controller item highlighted in the Hierarchy, drag and drop the GameOver (NOT the GameOver-Text) panel onto the empty Game Over Screen slot in the Inspector:

Dragging a game over screen from the Hierarchy to the Inspector in Unity 3D

Try it out now. Lose all your lives and you should see the game over screen appear.

 

In the next lesson below, we'll have a look at placing multiple enemies in the scene.

<--Back to the Unity 3D Course Contents Page