Speak to me! — A quick guide to creating a user interface in Unity

Luke Duckett
3 min readApr 14, 2021

--

The easiest form of feedback to the player is through a UI system. The player does not need to guess how much health or ammo they have. We can display it right there on the screen. Who doesn’t love a high score on a screen slightly better than your friends so you can brag?

In Unity, we have an easy-to-use UI system built in with a myriad of options to select from.

We will build a score and lives system in this article following our space shooter example. We are going to avoid text mesh pro in this project but it will come up in a later article.

For our score system, we just want to add some simple text to our scene.

Our UI elements need to be on a canvas to function, notice when we create the text the canvas is automatically created for us by Unity.

I’m going to use this canvas as our UI Manager as well so I will rename it as such. We will also need a UI Manager script, so go ahead and create one and add it to your game object (canvas).

UI elements do not work out of the box in Unity, so we need to import the UI library to use if we want to access our text components.

We will create a score value on our player which, using script communication we will add to every time we destroy an enemy.

We are also using the singleton game design pattern to reference our UI Manager. We will discuss this in a later article.

Notice how we have a parameter here of an int value. If you remember our modular scripts article you will notice that we are setting this function up to both add and subtract values.

Over on our UI Manager, we have added a simple function that replaces the score text with the current player score value.

Now we can watch it in action

Next, we can do the same with our lives display. We need to add an image first.

We have a set of sprites that we are going to use to represent the remaining lives. What we will do is set them into an array and then change the sprite in the image based on our lives.

We also add the function to push the information from the player.

All that is left is populating the array on our UI Manager and then we test it all together.

That’s all for now.

--

--

Luke Duckett
Luke Duckett

Written by Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology

No responses yet