Building UI in Unity

Creating a Score board and Lives Counter (part 3)

Now it is time to link the lives UI to the script and have the script display the correct amount of lives left after being hit by an enemy. To do this, we will be utilizing something called “Arrays” and we will have the script swap out the correct sprite within the array.

How Arrays work?

Arrays are variables that is able to collect and store other types of variables within a list. To use an array, basically type “[]” behind a variable to create the array. Within the inspector you are able to set how many elements the array will have, in player’s life example we shall set the amount of elements to 4.

example of an Array

After you have assigned how many sprites are going to be within the list, we will need to have the UI Manager script update the corresponding element to the player situation. (ie. if the player has only 2 lives left, the script will use Element 2).

Updating the Array Index

Within the UI Manager script, we will be creating a new public void method called “Update Lives” with the integer parameter “current lives”. Then within this method, we will need to firstly display the image sprite (ie. 3 lifes) then swap it within the array index.

Using the image handle we can utilize the “.sprite” we can get or set a function for the handle. In this case we call upon the array and have it access the image within the index.

Finally within the player script, we call upon this method within the Damage method using the UI manager.

--

--

--

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to pick your first workload for a Data Lake?

How to build NFT marketplaces part 2: Backend

RxJava to Coroutines on Android by Example

QA Engineer’s Journey to Scrum Master in a Mobile Team

Prepare for AZ-900 Microsoft Azure Fundamentals Exam in 2021

What the Fudge Is a Route Resolver in Angular?

How to Use Fairwinds Insights to Identify Kubernetes Cluster Misconfiguration

The Awesome Python repo is the second entry in our list of top GitHub Repos for

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

More from Medium

Flutter: How to detect user inactivity | Clarika

Building a cryptocurrency app with flutter_bloc 8.0

BlaBlaCar’s Translation Difficulties

Case Study Website Apps Infak or Donation