This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila
The Unity UI is a user interface development toolkit for games and applications. This framework is based around gameObjects that arrange, position, and style user interfaces using Components and the Game View.
In today’s article, we’ll explore how to easily add a UI element and script it into a fully functional Score System!
Adding Score Text
- Go to the Hierarchy, and right-click to open up a drop-down menu.
This adds a Canvas, a text object, and an event System to the hierarchy. The Canvas is our workspace where all the UI elements reside, and all the UI elements, like the text we just added, are children of this primary gameObject. The eventSystem is what makes the UI interactable.
Once you zoom out, you will be able to see the canvas and your newly added text at the bottom.
2. Change the color of your text to make it more visible if necessary.
3. Position your text: The perimeter of the canvas represents our Game View screen’s dimensions. This means that if you place the text at the top-left corner of the canvas, it will appear at the top-left of your screen in the Game View as well.
4. Anchor the position of your text: if your text is not anchored to a specific position when the screen is resized, the text position won’t stay in place. Anchoring your text is essential when deploying to different platforms where screen sizes can vary.
You can anchor your text using the Anchor Presets in the Inspector. This allows you to select a specific canvas quadrant or position to place your text in. Then, you can adjust the positions, width, and Height as necessary.
5. Scale text in perspective to screen size: It’s important to keep in mind that the text will stay the same size no matter the size of the screen. This can result in over-size or tiny text depending on the display it’s on. To ensure the text scales in perspective to the screen size, you can set the UI Scale Mode in the Inspector.
Scripting the Score Text
In this section, I will show you how to script your text so that the content within it is updated via code. This example uses the 2D Space Shooter where the 10 points are added to the score each time an enemy is defeated.
- Create a script to handle the UI management and attached it to the Canvas. We’ll be using this script soon!
2. Create a global variable to store the score. My score variable is located in my Player script because the score is added to the player.
3. Add a method to add points to the score. This must be in the same script where your score variable is located.
Note: You can directly use an int value to add to the score. The example below uses a paremeter variable that can be tweaked from the Enemy script to assign diffrent point values per enemy type if desired.
4. Use Script Communication to access the addScore method from the respective script. In my case, I access this method from the Enemy script because the score needs to update every time an enemy is defeated. The value in the parenthesis will be the points from the previous variable.
Note: In situations like this, it is best practice to check if the object or component you referenced via script communication is null.
5. In the UI Manager Script, make sure to add the Unity UI library to your so that you can access and manipulated the text through code.
6. Create a handle to reference the text object using a SerializeField to access it from the Inspector. Drag the text object from the hierarchy to the newly created reference.
Note: Since you previously made the Unity UI library accessible, now you can create text data types. This is what makes it possible to add a Text variable here.
7. Set the starting score to 0. Since this is using a text data type, it will actually display on our UI.
8. Create a method to update the score via UI Text in your UI Manager Script.
9. In the Player Script(or the respective script where you added the addScore method from 3), use script communication to access the updateScore method. This is done so that every time there is an addition to the score, it will reflect in our UI as well.
Now, you should have a fully functional Score system that will display in the UI!
In my next article, we’ll take a look at how to make a UI to keep track of the player lives with actual sprite images!
Creating a Score UI Element in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila
Dennisse Pagán Dávila | Sciencx (2021-04-29T01:21:09+00:00) Creating a Score UI Element in Unity. Retrieved from https://www.scien.cx/2021/04/29/creating-a-score-ui-element-in-unity/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.