Reader small image

You're reading from  Unity Certified Programmer Exam Guide - Second Edition

Product typeBook
Published inMay 2022
Reading LevelIntermediate
PublisherPackt
ISBN-139781803246215
Edition2nd Edition
Languages
Right arrow
Author (1)
Philip Walker
Philip Walker
author image
Philip Walker

Philip Walker originally started as a 3D game artist, but then decided he wanted to combine his current skills with coding so that he could see through the majority of his games' and apps' development himself. Philip has worked in various different industries as an artist and as a Unity developer using various types of technology and techniques.
Read more about Philip Walker

Right arrow

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:

  1. 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...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Unity Certified Programmer Exam Guide - Second Edition
Published in: May 2022Publisher: PacktISBN-13: 9781803246215

Author (1)

author image
Philip Walker

Philip Walker originally started as a 3D game artist, but then decided he wanted to combine his current skills with coding so that he could see through the majority of his games' and apps' development himself. Philip has worked in various different industries as an artist and as a Unity developer using various types of technology and techniques.
Read more about Philip Walker