Step 11.5 — UI Elements Continued

Nicholas Lim
2 min readMay 1, 2021

Showing the lives of the player via sprites is a similar series of steps.

In the Hierarchy, right click → UI → Image to create an Image object under the Canvas.

As with the Text, the two things to note are the Anchor for this sprite, on the top left this time, and the Source Image, which we will set as the three ship image seen at the top of this article. There is also a Preserve Aspect in the event the sprite’s proportions are weird.

Just as the Text’s display is updated in code as the game runs, so too does the sprite displayed by the Image object change as the Player loses lives. For that to work the UIManager script has to know of four different sprites (representing full lives to none) to switch out. So in the UI Manager script we create Serialized array of Sprite objects to store those.

UI Manager script

The public UpdateLives method means that once again the Player will be calling this method and passing it the number of lives left as the int argument, and the Image object will update with the corresponding sprite.

--

--