Game Over!!
What happens when you run out of lives??
Adding the lives meter
Let’s add some more stuff to our canvas!! How about a lives counter??
The goal of our meter is to swap out the sprite of an image between four sprites depending on how many lives we have left.
I added all the images to a GameObject to separate them out by component area. Our script will need access to the Sprites so let’s create an array to store them. We also need reference to the Image we are going to be updating.
In order to process updating the Sprite of the Image let’s setup our update method.
Now that our UIManager is setup let’s setup our player to call UpdateLives. It should already have access to the UIManager from our work on the score component. Now we will add the UpdateLives to our DamagePlayer method.
Adding the Game Over Text and Flasher
Let’s setup some Game Over text. First we will need a reference to the Text and a Text UI Component.
Then we need to setup our action method to handle the changes to visibility of text.
Over in Player we need to update CheckLives. Again Player already has a refernce to the UIManager so we can tie into that.
Here there is a reference to FlickerText which is the next step. FlickerText enables the GameOver text and then starts a coroutine to handle the text active flipper.
Restart with “R”
Finally, our last stop of the day… Or is it the beginning again??
First we need to create the Restart Text and then add a reference to the UIManager so we can call it on death.
Adding our enable functionality is also important.
Since we only want to restart when the Restart Text is visible we will create a GameObject and attached it to the text component.
This script will not run until the Restart Text becomes active which means we have to do fewer checks putting it here.
SceneManager.LoadScene loads a scene either by id or name.