Player’s Lives UI + Game Over & Restart in Unity!
Today, I will show you how to set up Player’s lives UI. We will have a total of 3 lives when we are starting the game, then lose one at a time when the player collides with an enemy.
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).
In the Inspector window, let’s also make sure to link the four sprites in the correct order.
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.
Let’s take a look at the result so far.
Cool, now we have the player’s lives UI working properly!
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.
Now, we can see below that the Game Over and Restart texts appear when the player dies.
Great, but feels like something is missing? Let’s add flicker effect to Game Over text!
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.
We are going to use use StartCoroutine() when GameOverSequence() method is being called. Then, in IEnumerator, we will SetActive(true) for 0.5 second & SetActive(false) for 0.5 second.
*But, it will not repeat again, because we are missing ‘While loop’ that will keep repeat until the condition is true. We can create and use a bool type variable _isRestartActive == false)
Restarting the Game with ‘R’ key
The last step is to create a function that will restart the game when we press ‘R’ key.
- Create a new empty game object, called ‘GameManager’.
- Create a new C# script called ‘GameManager’.
- In ‘GameManager’ script, we need a bool type variable ‘_isGameOver’ that will be called out to be ‘True’ when GameOverSequence() method is running.
- Create a new method called ‘GameOver()’ that will turn the bool variable to true.
- 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.
Then, in GameOverSequence() method, we will say ‘_gameManager.GameOver();’.
And, the result is looking great!
Thank you so much for reading!