Today I will be showing you how to create a counter that displays how many lives we have using sprites.
Attach an image game object to your canvas and re-name it.
Drag and drop your lives display sprite into the Source Image empty field.
Go ahead and constrain your image to your screen under Rect Transform. I’ve constrained mine to the top left. We’ll also want to make sure ‘Preserve Aspect is ticked. This ensures that our image will preserve it’s size and not get blurry when on different device sizes.
Next, I’ve created a lives array in my UI Manager script.
On our canvas game object, I’ve set the ‘Live Sprites’ size to 4 to fit all our sprites.
We need a handle so we can switch between sprites. As well as a method that will call our sprite handle.
In the UIManager, we will do just that.
In our Player script, when we get hit we will need to call our lives method.
Now when the we get hit by the enemy, our lives will display how many we have left until game over!