Creating a Retro Game Over Behavior

Player lives and Game Over screen

Objective: Create a Lives counter in the top left corner of the screen and create a Game Over screen to play when the player dies.

The first thing we need to do is set up the UI. Begin by creating an image in the Canvas and name it Lives_Display_Image.

create an image for the UI
Name the Image

We need to anchor the image to the top left of the screen, adjust the padding, and drag in the sprite. Also, we need to enable Preserve Aspect to make the Image not look stretched out.

Now in the UI Manager Script, we need to reference all the variations of the lives we will need for 0,1,2,3 lives and add in the sprites from the Unity inspector:

private Sprite[] _liveSprite
Unity inspector

We will also need a reference to the Image component of the Live_Display_Image.

private Image _livesImg

The last thing we need to do for the lives system is to make a UpdateLives method callable from the Player script when they take damage.

UI Manager UpdateLives method
Called in the Player’s Damage method

Create a text object in the Canvas and have the text “Game Over” written in white. We want the text to be large, and to do this; we can change the font size to 50 and set the Horizontal/vertical Overflows to “Overflow.” Also, disable the object because the player starts with lives.

Adjust Text and set Overflows

The last thing we need to do is in the UpdateLives method; if the lives are equal to 0, display the text:

Display Game Over text

Learning to become a unity game developer