Creating lives for the player
In this section, we are going to make it so that the player has a set number of lives. If and when the player collides with an enemy, the player will die, the scene will reset, and a life will be deducted from the player. When all the lives are gone, we will introduce the game over scene.
We will be working with the following scripts in this section:
GameManager
SceneManager
Player
Let's start by revisiting the GameManager
script and setting up the ability to give and take the player's lives:
- Open the
GameManager
script and enter the following code:public static int playerLives = 3;
At the top of the script, just after entering the class and inheritance, enter a static
(meaning only one) integer type labeled playerLives
, along with the value 3
.
Next, we need to create a new method for our GameManager
script that will ensure the player loses a life. After we make this new method, the Player
script will...