Lives Counter in Unity

Tutorial for making a Lives Counter.

Kozmobot Games has published a new tutorial on his YouTube channel. This tutorial is about a lives counter in Unity, based on displaying the number of hearts on the screen. The developer uses a switch function that defines what to do when the variable health, which is a static int, becomes lower than one.

In this tutorial, you will learn how to make a health system in Unity, by simply adding a lives counter to your scene. Follow these steps!

Click the video to watch.


To make the heart symbol, go to video description to download the asset image. Then, while importing make sure to select Sprites / 2D and UI option and you are good to go.

Add a heart symbol simply by going to Canvas / UI / RawImage and in the texture field, click and drag the heart symbol and the regular white square becomes a red heart symbol. You got it!

Heart Symbol

When adding a heart symbol, you can customize it to whatever you want. The heart symbols in Unity can be a symbol that can only act as a sprite. In this case, hearts are a game object. If no hearts are on the screen, the game over the screen will be displayed. To do this, you simply need to open a script and define switch(health) function in void Update. The game objects that you should define are: heart0, heart1, heart2, heart3 and the game over object in the main game canvas.

To do that, first create an empty game object, call it Game Manager and add a script of the same name. Then you can open Visual Studio and start adding your code!

This tutorial can also be considered a Game Over tutorial because there is part of adding a game over screen and reloading the scene.

lives counter gameover

Game Over

There are two objects: a text mesh and a button, which is part of text mesh. To add these two elements, import TMP essentials into your project. Then, add an empty object and name it game over object, which will be used in the script…

In default, you need to add Time.timeScale = 0; and the last line of code is break, because the script cannot be saved without it.

Other scripts

In the respawn script, defined in a tutorial that I created before, you can change FixedUpdate to Update and add -= 1;

In MovementScript, add another line of code, to detect how the player reacts after hitting a bullet:

The restart level script is attached to the game over object. Make sure to add SceneManagement before making the Scene Manager in your project. That will enable your project to reload scenes. Then you should add your scene in the Build Settings.

Stay tuned!

Kozmobot Games is a young YouTube channel that recently celebrated crossing the 200 subscribers mark. Subscribe to the channel here. Be the first to know about releasing new videos and improving your game development skills!


Leave a Reply

Your email address will not be published. Required fields are marked *