Objective: Create a visual system with the UI Manager that shows how many lives the player has left before game over.
Like the score system, this will be taking place within our canvas object. This time, however, we’ll be using an image instead of text. So right click on canvas, go to UI, then click image.
Now since we’ll be starting with three lives, find the sprite you want to represent full health and drag into the source image box. Also make sure “preserve aspect” is checked, so that the image doesn’t appear stretched or squished when it comes on scene. Once this is done, move the image where you want it, and choose an anchor point to lock it’s scaling position.
Now go to the UI manager script and create a variable for the life display, and an array where we can attach the sprites for the other lives.
In the inspector, drag the Lives_Display object into the _lifeDisplay box. _liveSprites should have a length of 4, and drag the appropriate sprite into each one. Zero lives go in element 0, one life goes in 1, etc.
Now create a method for UpdateLives, and the code should look like this:
The code in the () basically turns the value we’ll be getting from another script into a variable we can use in this method. Let’s go get that value, so go to the player script. We want to call this newly made method where the value of life changes, in the damage method.
This should be all we need to get the function working.