Game Over! It- Time to try again!

Out of lives? Replay to continue the fun!

Timo Schmid
Nerd For Tech

--

Every game nowadays has some kind of Game Over screen. Have no lives left? Have not achieved the main goal within the time limit? Out of ammo and can’t defend yourself anymore? Such situations mostly lead to a Game Over screen or at least behavior of the game. As for the Space Shooter, you’ll face the Game Over screen when you are out of lives.

Let’s get this done!

Preparing to code
Before we get started to implement the logic here, we need some textboxes in which we write the text to.

  • (2x) Right-click the Canvas > UI > Text
  • Rename the GameObjects to make it clear which textbox it is. We need a textbox saying “GAME OVER” and one telling the player how to restart the game.
  • Change the text to “GAME OVER” and “Press ‘R’ to restart!”
  • The default text is pretty small. Change the size to make it look attractive!
    I used size 80 for Game Over and font size 28 for the restart text
  • You might have noticed that the text seems to have disappeared. That’s because the text is bigger as the textbox area. We can do two things here:
    - (1) Use the Rect Tool (‘T’ key as shortcut) to scale the textbox so the text is fitting
    - (2) Change the Horizontal Overflow and Vertical Overflow to “Overflow”
  • Place the text where you think they are fitting.
  • Tune colors and font to your liking if needed

Having done that, your Game view can look like this:

Coding the flicker behavior
Now it’s time for the fun part and actually give these text boxes some behavior through code!

We begin as always, we need two variables of type Text. As we store everything UI related into the UI Manager, we will put those handles inside the UIManager.cs script as well:

Now then, what do we want to do with them?
We want to create a flicker effect. There are multiple ways to achieve a flicker effect like this but as we want to keep it easy, let’s just change the text displayed!

  • A flicker effect is repetitive. Therefore, we need to change the time after a specified amount of time. A coroutine works perfect for this!
  • We don’t know how long the player will see this screen or when a replay is triggered. Thus, we need a endless while loop.
  • In this loop, we need to change the displayed text.

This will be the logic we just wrote down:

Calling the method
The logic is there, but when do we actually need it?

  • The player has 0 lives left
  • This will cause the method to be called

Remember the switch statement we created to visualize the current player lives as sprites? We left out the case 0 part. Now it’s time to fill that in and call the coroutine!

  • We simply need to enable both textboxes and let them flicker then

Inside the UpdateLives([…]) method, add these lines into case 0:

Disable the GameOver message at restart
The textboxes are flickering now. However, when we restart the game, the textboxes are not going away.

This is fixed pretty easily. We simply need to disable both GameObjects as soon as we start the game. Let’s create a new method for this and call it in void Start():

Now we have the intended behavior:

The ability to restart
The player won’t have the Unity Editor to simply restart the game. We need to create some logic for that!

  • If the player is at the Game Over state and presses the R key, the game should reload
  • If the player is not at the Game Over state, pressing the R key should do nothing

In order to check the current state, we should create a bool variable. But, instead of putting it into the UIManager script, we will create a new GameManager script, which we will use whenever we want to use scene management.

  • Create a new GameManager GameObject
  • Create a new script called GameManager
  • Attach the just created script to the GameManager GameObject
  • Open up the script

We want to check whether the player has a Game Over or not. That’s easily done with a Boolean! Also, the Game Over sequence is stored inside the UI Manager. Therefore, we need a handle to the UI Manager script.

  • Create a bool which checks for the Game Over state
  • Create a handle to the UIManager script

Having done that, your variable section of the script can look like this:

Creating the actual logic
Begin as always.

  • Create the handle to the UIManager
  • Null-Check the handle!

Now it’s time for the restart logic!
If the player presses the R key and is in the game over state, then reload the scene

Breaking this down into little pieces we get the following things we need to check:

  • Check for the Game Over state bool
  • Check for a keypress of the R key on the keyboard.
    Remember that a key press can happen at any time!
  • If both apply, reload the scene

We check for two conditions which have to be the case. Otherwise, we don’t want to check for input on the R key. Perfect usage for an if statement!

If you want to mess around with scenes, Unity has a method called SceneManagement integrated. To be able to use this method, you need to integrate the UnityEngine.SceneManagement library at the very top of your script.

Changing the bool value at the right time
Finally, we need one last little method which will allow the player to actually restart the game. As we want to call it from the UIManager.cs script we need to make this a public method.

The purpose is simple. Set the bool to true.

That’s all we need!

Small last details to make it work as intended
We want to call the method as soon as the player has no lives left. The check for the remaining lives is inside the UIManager.cs script.

  • Open up the UIManager script
  • Search for the UpdateLives([…]) method
  • Inside the switch statement, click inside case 0
  • Call the just created GameOver() method on the GameManager.

You might have noticed that you will receive an error.

That’s okay. Let’s take a look at why we get this error.

  • The method we try to access is inside the GameManager script.
  • We want to call the method from the UIManager.
  • However, the UIManager script does not know that we have a GameManager script.
  • We don’t have a handle to it and therefore, cant access the methods inside the script.

Luckily, this problem is easily fixed. We just need to create a handle to the GameManager script in the usual way. Variable > Handle > NullCheck

It works!
Now everything is set up and should run as intended!

Let’s take a look at the end result:

--

--

Timo Schmid
Nerd For Tech

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)