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

Steps — Player’s Lives

1. Prepare sprite images

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

  • 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

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

Restarting the Game with ‘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




XR Unity Developer / Designer / Architect

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Python with Django — No Need to Reinvent

The Frog, the Software, and the Quality

LeetCode 19 — Remove Nth Node From End of List

Go lang: MongoDB, Iris API Part-1

Swastik Animation using HTML and CSS 💥

Functional Programming with Haskell

Ideal Relationship with your Engineering Manager

Zenity Command In Linux

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

More from Medium

In just 4 steps, Make a modular moving platform in Unity

How to Switch Virtual Cameras Using Only Cinemachine and Scripting in Unity

My journey becoming a Unity game developer: Audio Manager: Intro Cutscene Music Fixed and Cleanup

Creating a Simple Cooldown System In Unity