Player Lives

Following on from the previous lesson, we're now going to display the number of lives a player has. It will look like this:

Unity Game view showing health and player lives scores

Let's get started.

In the Hierarchy, expand your Panel, the one under Background. Select the Text-Health item. Hold down the left ALT on your keyboard. With the left ALT key held down, select the Text-Score item. Now right-click on the selected items and duplicate them. Rename them to Text-Lives and Text-Num. Here's what your Hierarchy should look like:

The Hierarchy showing canvas text elements

Now, in the Inspector, change the text for Text-Lives to Lives:. Change the Pos X values so that the text is on the right in your Game view. Do the same for Text-Num. Delete the text and type 3 instead. Change the Pos X value. It should look like the image at the top of this page, with Lives: 3 at the end.

Now go back to your PlayerHealth script. Add these variables at the top of your code, with all the others:

public TMPro.TMP_Text uiPlayerLives;
private bool gameOver;
private int numOfLives;

The first one is just like before, a public variable for the TextMesh Pro text. We've called it uiPlayerLives. (We'll assume you used TextMesh Pro for the text.)

The second variable is a public bool called gameOver. We're going to set this to true when the number of lives reaches zero.

The third variable is a private int called numOfLives. We'll deduct 1 from this when the player loses a life.

In your Start method, add these two lines:

gameOver = false;
numOfLives = System.Convert.ToInt32(uiPlayerLives.text);

First, we make sure that the gameOver Boolean is set to false when the game starts. The next line grabs the text from the screen. Notice, however, because you're grabbing text, you need to convert it to an integer (ToInt32). Once its converted, we store the value in the numOfLives variable.

Now copy and paste this code over your PlayerTakeDamage method:

public void PlayerTakeDamage(int damage)
{

if (gameOver == false)
{

playerHealth = playerHealth - damage;
updateHealthText(playerHealth);

if (playerHealth <= 0 && numOfLives >=0)
{

playerHealth = maxHealth;

playerPos.position = new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z);

updateHealthText(playerHealth);
numOfLives--;
uiPlayerLives.text = numOfLives.ToString();

}

if (numOfLives == 0)
{

gameOver = true;
Debug.Log("GAME OVER:" + gameOver);
//DISPLAY GAME OVER SCREEN

}

}

}

The code in your editor should look like this (new lines are highlighted):

Unity C# code for player lives and game over

Now let's go through the new code.

The code inside of PlayerTakeDamage is now wrapped in an if statement. This one:

if (gameOver == false)
{
}

We only want to player to take damage if the game isn't over. If it is game over, we'll display a Game Over screen shortly.

The first two lines in the if statement are the same as before:

playerHealth = playerHealth - damage;
uiPlayerHealth.text = playerHealth.ToString();

We're displaying the player's health score on screen. Next, we have another if statement:

if (playerHealth <= 0 && numOfLives >= 0)
{
}

The code for this if statement only gets executed if the playerHealth variable is less than or equal to zero AND the numOfLives variable is greater than or equal to zero. In other words, if the player has no health left, but it still has lives, we'll executed some code.

The new lines are these:

numOfLives--;
uiPlayerLives.text = numOfLives.ToString();

We have to deduct 1 from the number of lives, if the player's health has reached zero. We do it with this line:

numOfLives--;

We're using two minus signs after numOfLive. Two minus signs are the shorthand for decrement (reducing a value by 1). It's the

same as saying this:

numOfLives = numOfLives - 1;

After we take 1 away from numOfLives, we can display the result in our new text label:

uiPlayerLives.text = numOfLives.ToString();

The only other code is this if statement:

if (numOfLives == 0)
{

gameOver = true;
Debug.Log("GAME OVER:" + gameOver);
//DISPLAY GAME OVER SCREEN

}

Simple enough: if the player has no lives left, set gameOver to true. The other two line are a debug line (so you can see if it worked or not) and then we have a comment. The comment is where we'll display a game over screen shortly.

Save your code and go back to Unity. Make sure your First person controller is selected in the Hierarchy. Now drag and drop Text-Num onto the empty UI Player Lives slot in the Inspector:

Dragging a text element onto the Inspector

Before you play your game, for testing purposes, you may want to change your Max Health to something smaller. In the image above, we've set it to 30. When the game starts, the Player Health will then be 30 as well.

Click on your Console tab at the bottom of Unity. Now play your game. When you lose all 3 lives, you should see the Game Over message appear in the Console. This came from your Debug line.

OK, now let's do a game over screen.

<--Back to the Unity 3D Course Contents Page