Creating Lives System (part 2)

How to subtract lives from death

Now that we have created the basic lives system, where the total amount is being calculated by the player script and also shown in the UI thanks to the UI manager script. We now need a factor which will allow the player to subtract one life for each death/accident, and also create an respawn system where the player automatically gets teleported back to the start location upon death.

Setting Up the Player Script:

Following the same method as the coins system, we are going to create a new public method which takes our current lives and minus 1. Then we are going to access the UI Manager and reference the Update Lives Display method which we will create later.

In the UI manager script, we are going to create that public method “Update Lives Display” by following the same method as the score system.

Creating the Boundaries:

Back in Unity, we need to create the physical boundary which will trigger the subtracting of the lives and also resetting the player position.

Create a new script and named it “Death Behavior” since this will be a behavior script. What we need to do is set up an empty game object with an transform position which will be our respawn location. Next we need to access the Character Controller because unlike the rigid body component, we need to manually turn off and on the controller to allow the teleportation to happen.

Also at the Start method, I referenced the Character Controller on the player object.

The logic behind the boundary is to have the player collide with the barrier and have two things happen, a) the Lives counter goes down by 1, b) The player resets their position back to the respawn location. Therefore we will be using an On Trigger Enter method to trigger these events.

After null checking to see if we have access to the player component, we are going to reference the public method from the player script that subtracts the lives. Also at the same time we are going to disable the Character Controller.

We reset the position of the player and to activate the Character controller back online again, we need to use an IEnumerator to wait for 0.5 seconds before the Character Controller is utilized again.

Resetting the Character Controller using an IEnumerator

Remember to star the coroutine right after you reset the player position.

Back in Unity, drag and drop the respawn location game object into the script slot.

Resetting the Scene After a Game Over:

Finally, we are going to restart the scene when the lives = 0. In the player script, we are going to use the Scene Management library, so we have access to loading in different scenes. For this purpose we are going to load in scene Zero, which is the current scene.

In the public Death method, we are going to add in a IF statement to state a condition when Lives ≤ 0, reload the scene ZERO. When that happens, the score and Lives will be reset to default therefore giving the impression of restarting the game.

resetting the game.

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

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

Recommended from Medium

PSD2: API Documentation Available

Docker Containers First Step

How to create ‘Component/Library’ in ESP-IDF for ESP32

How I set up a testing environment using pytest

cs371p 2020: Final Entry

Distributed Edge Computing and Volterra

Most Useful Widgets for your First Flutter App

Clean architectures & hybrid development.

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
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

More from Medium

2D Player Animation (part 2)

3rd Person Controller Using Cinemachine Virtual Camera On Mobile


Unity Beginner — Day 03