Creating a Lives System in Unity (2.5D Platformer) [1]

I recently needed to create a lives system for this 2.5D platformer I’ve been working on, and will be splitting this up into two parts: one for storing, updating, and displaying lives, and the other for ‘killing’ the player and respawning/reloading accordingly 😄

Objective: Create a system for storing, updating, and displaying the number of lives the player has.

Over on our Player class, let’s start by creating a private integer called ‘_lives’ and initialise it with a value (in this case, I’m going with 3). If we want to get a bit fancy, we could use a public property to essentially give other classes read-only access to this private _lives variable. We might as well implement this, as I’m hoping to only update the number of lives via a public method on this class.

The public property for this isn’t essentially but could come in handy.

Now let’s create our public PlayerDied method. This’ll get called from other scripts, for instance from a ‘DeadZone’ object in our scene (i.e. for whenever the player falls off the level). For the time being we’ll just be checking to see if their current lives is greater than 0, and if it is we’ll be decrementing their lives, and calling a custom Event (which we’ll get into shortly).

Over in the singleton GameEvents class mentioned in a previous article, I’ll be using one of my custom ‘IntEvents’ to essentially broadcast the player’s remaining lives. This’ll entail both the IntEvent and a public method to be called (as it’s getting called in the PlayerDied method above):

Now all I want to do is make some adjustments to my UIManager to support the storing, updating, and displaying of the player’s lives. As with the coins, I’ll be listening for one of these IntEvents and updating a local integer variable and text display accordingly. I’ll skip over most of the code from the coins and just cover the Lives stuff here:

Finally, we can hop back into Unity, let it compile, and create and drag in a TextMeshPro text object to the available _livesText slot on the UIManager. Now assuming we have the means for calling the PlayerDied method on the player, we should be able to update and broadcast the player’s remaining lives using events and display them back via the UIManager. I’ll touch on calling this, and on adding in some initial respawn/reloading functionality in a subsequent article 😉



Game Developer | Game Design and Literature Graduate

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