Player’s Lives UI + Game Over & Restart in Unity!

Steps — Player’s Lives

1. Prepare sprite images

So, first, we need to prepare 4 sprite images that represent each amount of player’s lives.

2. Create UI Image under Canvas

  • We need to create UI Image under Canvas that will be linked to those 4 sprites above. Right-click on the Hierarchy window → UI → Image. Rename this image as ‘PlayerLives’.
  • Then, drag and drop the sprite with 3 lives into ‘Source Image’ under Image Component. The sprite may look stretched. Click on ‘Preserve Aspect’ to keep the image ratio locked in.

3. In UI Manager Script

  • Let’s jump into UI Manager Script. First, we need to create two variables: One for UI Image that we created in Step 2 to show how many lives we are left with. Another one (Array type) will hold 4 sprite images of player lives (0 as zero life to 3 as three lives remaining).
  • Next, create a new method called ‘UpdateLives()’ with an int type parameter that represents the current lives remaining. Within the method, we will write ‘UI Image = playerLive[current lives]’. And, if the current lives is 0, then perform GameOver (we will go over this very shortly).

4. In Player’s Script

Now that we the method that will show the current lives sprite, we need to be able to call this method from the player script when the player is damaged.

  • First, let’s create 2 variables: one to represent the lives when we start the game (_lives = 3), another one to link UI Manager script.
  • We will also need to make sure to add GameObject.Find(“UI_Manager”).GetComponent<UIManager>(); in void Start(), so that we can grab and link UI Manager Script.
  • Under Damage() method, currently, we lose 1 life each time (_lives -= 1;). After this, we need to add _uIManager.UpdateLives(_lives) to update the sprite to show the correct amount of remaining player’s lives.

Game Over Screen

The next step is to show Game Over Screen when the player dies.

1. Create ‘Game Over’ & ‘Press R Button to restart’ texts

  • Create a new text by ‘right-click from Hierarchy Window → UI → Text’. Let’s rename it as ‘GameOver_text’ + write ‘GAME OVER’ in the text field.
  • Create another new text by following the first step above. Rename it to be ‘Restart_text’ + write ‘Press the ‘R’ key to restart the level’ in the text field.

2. In UI Manager script,

  • Write a new method called ‘GameOverSequence()’ that will turn on (SetActive(true) the two texts (Game Over + Restart).
  • Call this method above when ‘currentlives == 0’.
  • Turn off the two text game objects from the inspector window.
Do the same for the restart text

Flicker Effect

It’s pretty simple to create a flicker effect. There are several ways to do this, but, for today, let’s use SetActive() to turn on and off.

Restarting the Game with ‘R’ key

The last step is to create a function that will restart the game when we press ‘R’ key.

1. GameManager

  1. Create a new empty game object, called ‘GameManager’.
  2. Create a new C# script called ‘GameManager’.
  3. In ‘GameManager’ script, we need a bool type variable ‘_isGameOver’ that will be called out to be ‘True’ when GameOverSequence() method is running.
  4. Create a new method called ‘GameOver()’ that will turn the bool variable to true.
  5. In void Update(), if ‘R’ key is pressed AND _isGameOver == true, then, load this game scene again. *IMPORTANT 2 steps below.
  • To load a scene, we need to use SceneManager.LoadScene(), BUT in order to use this method, we must add a namespace, ‘using UnityEngine.SceneManagement;’.
  • We can just use a string type “Game” to load the scene, but it is better and more efficient to use an int type. Go to Build Settings → if you press ‘Add Open Scenes’, you should see Game Scene is indicated as ‘0’ (assuming that you have the Game scene opened, if not you can drag and drop the scene as well). And, finally, we would write SceneManager.LoadScene(0).

2. In UI Manager script

In UI Manager script, we need to call the method ‘GameOver()’ in GameManager script. Let’s create a variable & GetComponent for ‘GameManager’ similar to how we did previously.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Du Young Yoon

Du Young Yoon

XR Unity Developer / Designer / Architect