Level Up Your Unity Skills: Updating and Displaying the Score Count

Gerald "Ray" Patton
5 min readApr 5, 2024

--

Objective: Create a system to display the Score every time an Enemy is destroyed.

This process requires that we create a new UI(User Interface) Manager system. A UI Manager is responsible for updating every thing related to the screen display, including things like the Score, Lives Counter, Ammo Count, etc...

To create this UI Manager system, I realized the UI is basically run as part of our Canvas, we create a new Script and attach to our Canvas. This script I choose to name UIManager.

I had to use the Player, UIManager, and Enemy Scripts to be access the Scoring system in order to work and display properly, but before I begin code these, UnityEngine.UI and TMPro libraries needed to be added to the UIManager Script.

I was using Unity 2022.3.16f1 when creating this.

Step 1 — Create Score Variable:

The Score is a variable that doesn’t exist yet, so I created one. This was done within the Player Script and add new variable with an integer data type and a SerializeField class:

Step 2 — Add to Score Value:

While still working in the Player Script I created a new method that allows communication between both this script and UIManager Script and updating the score after the Enemy is destroyed.

This method is called from the Enemy Script whenever Laser collides with the Enemy gameObject. After this happens the points parameter will allow the Enemy Script to determine the point integer value for each Enemy, while not just hard coding one value for every enemy.

Step 3 — Update the Score:

To add these ten points access to the Player script within the Enemy Script was created. I created both a Global Reference variable for our Player and a handle to the _player.

After the handle _player is created, I was able to assign it to the component, using the GetComponent method. Assigning and using the handle to the GetComponent in this way caches the handle, allowing it to be called multiple times without using excess memory in the code.

The handle can now be checked within on “Laser” tag in the Enemy Script. Adding an if statement that checks for a still active player and then AddScore, when one exist. The score value is added here, because this allows for more scoring capabilities or options to be coded later.

Step 4— View the Score:

I had created a working Scoring system the could viewed through the Player Component in the Inspector Window. But I needed it to be viewable on screen.

To accomplish this I need the Player Script to communicate with the UIManager Script I created earlier. I essentially needed to update the Text data.

To communicate properly I created an new handle with the UIManager Script called private TMP_Text _scoreText;. (As a reminder, if the UnityEngine.UI and TMPro libraries where not add before. They would need to be added for this and the following steps to work properly)

After creating this reference to the _scoreText; I was able to add it to my UI Manager Script component within the Canvas GameObject.

Still within my UIManager Script I now was able to code a reference that would allow for the Score text to be updated. I chose mine to display “Score: 00”.

With these created I now have a reference for the Player Script to use. And similar to how I created a Global Reference and handle to our Player in the Enemy Script, I created another Global Reference and handle. This time to the UI Manager within the Player Script.

New handle for our UIManager.

To get our Global Reference the UIManager component from within the Player Script, I first had to find it. The UI Manager component exist within my Canvas, so I coded it to reflect that.

I also created a Null check for when I run the game to check that the UI Manager exist.

From here, I hopped into my UIManager Script to created a method to UpdateScore for our AddScore.

Back to the Player Script I coded the ability for it to capture and display the update score with method from the UIManager Script within the AddScore method:

With that I have a working Scoring System that displays properly on the screen. Until next time, Happy Developing!

--

--

Gerald "Ray" Patton

Unity Developer | C# Software Engineer | Game Developer | USAF Veteran