Lives System

Kinga Osińska
4 min readJul 8, 2021

The next thing we are going to do is a live system. Our player will have three lives. When he falls, he will lose a life. If he loses all three lives, we will restart the game.

Create a new UI text called “Lives_txt”. You can duplicate the Coin Display text and just change the name and text.

Go to UIManager. Add Text variable called _livesDisplay. Assign it in the Inspector.

Create a public method that will update lives displayed in our game. When we call this method we will pass the number of lives the player has.

We need to know the location of the respawn point. Add an empty GameObject and name it Respawn_Location. Position it in the same place as the Player GameObject.

Next we will create a method in the Player script that will subtract lives from our player.

Open Player script and add a new variable called _lives.

Create a new method called Damage(). Every time we call this method, we will subtract one from _lives. We need to update the lives display and pass in the _lives variable. if the player has no lives left we will load a scene to restart the game. To use SceneManager you need to add:

Go to Files -> Build Setting and add the scene to be able to load the scene

Now it’s time for checking if the player has fallen down the map. There are a few methods to do it. We can check the position of the player on y axis, if the player’s velocity increased, or we can put a trigger along the map. We will go with the third option.

Add a cube and call it a “Dead_zone”. Scale it to match the length of the map and disable Mesh Renderer. In the Box Collider component check isTrigger to true and add a Rigidbody.

Create a new script called DeadZone. Add a new variable called _respawnPoint and assign it in the Inspector

We will check if the Player trigger the method, if he did, we will call the Damage method in the Player script and position our player to the respawn point.

When we play the game you will notice that if our Player fell, he will lose a life but he didn’t get to the respawn point.

This is because of the Character Controller component on the Player. We need to disable it when we fell and enable it when we restart. We need to access the Character Controller component on the Player and then, using the coroutine we will enable it after half a second.

--

--