Creating a Classic Game Over Screen

Wesley Campbell
3 min readNov 12, 2022

--

A game with any kind of challenge can be failed, so the game must reflect that and then move on from there. One classic way this was done was to write “GAME OVER” on the screen and show the final score before allowing you to start over and try again. Let’s implement that into our space shooter game, Interstellar Dogfighter.

Implementing UI for the Lives System

We already have a lives system set up and working, so let’s get the UI set up. First let’s get the graphic set up on the screen by creating a new UI image.

Creating a UI Image

We have sprites representing the number of lives we have left, so we’ll grab one to use as our default image for the sprite. Checking the Preserve Aspect box will fit your image in the box but preserve its original aspect ratio instead of stretching it. Let’s position it at the top left of the screen.

Setting a UI Sprite

We have different sprites to represent how many lives we have, so in code, we’ll list each of the sprites in an array. The first element will be the sprite for 0 lives, since the index is 0, and the last element will be the sprite for 3 lives, and the index will be 3. Then we can update the sprite when the player is damaged, and simply use the number of lives as the index for selecting which sprite to use from the array.

Updating UI Sprite in Code

Creating the Game Over Text

Next let’s create a new Game Over text element and turn it on when the player’s lives hits 0. Now, we want to actually make the text flash on the screen. There are many ways of doing this, such as using coroutines or an animation to turn on and off the game over object or the text component, or to use a coroutine to set the text to an empty string and back.

It’s a good idea to make this flashing behavior exist on the object itself instead of making another object control it, since it’s constant. Let’s create a coroutine that turns the text component on and off, since it’ll be easier to control the speed that way. We use OnEnable here instead of Start so that the coroutine can be called every time the object is enabled instead of just once.

Flashing Text Script

Now we just enable the game over object when the player runs out of lives and adjust our settings in the Inspector, and we have our classic game over screen!

Game Over Screen

--

--

Wesley Campbell

Ravenous game developer and storyteller. Though I do play games for fun, I also constantly analyze why games are fun or popular, as well as how to improve them.