Updating our Player’s Score

Frank Warman
4 min readApr 23, 2021

In the last article we created our Canvas UI — a place for our UI elements to live. We set up a Score text box, but it’s completely static right now.

In this article, we’ll be interacting with our Score through code, and updating in real-time when we destroy an Enemy!

New High-score!

The UIManager script

Create a new UImanager script to handle our UI code!

To handle our UI updates and the like, we’ll create a UIManager script and attach it to our Canvas.

Attaching to our Canvas

Now inside of our UIManager script, we have to add a new namespaceUnityEngine.UI. This enables us to use Unity’s pre determined code for it’s UI elements. Interacting with our UI Canvas would be impossible without it.

Namespaces are located at the top of the script

Then we can create a handle for our Score text! It will be of type Text, and we’ll Serialize it so we can link in the Inspector.

In our Start() function we can initialize our Score text to be displayed. We can use a combination of strings and numbers, and soon variables.

_scoreText created

Linking the correct Score text:

Make sure you drag in the Score Text, and not the entire canvas

And our Score text initializing at Start:

Pre-programmed Score display!

Inside the Player script

There are many places we could store our Total Score within the game. You could choose to add it in the UI Manager, but I will be storing it in the Player script.

So we’ll create a _totalScore variable, and also a function to add to our _totalScore:

To hold our Score
To add to our Score. Adds 10 every time this function is called.

And since we’ll be interacting with our UI Manager, we’ll create a reference to it, and null check in the Start() function:

Reference and null check

Interacting between the Scripts

We’ve created a reference to our UIManager in our Player script, but we’ll need a function to call from the UIManager to update our score display.

Create an UpdateScore(int totalScore) function. This requires an int to be passed in and be utilized.

This function will update our Score display with whatever int we pass through it.

UpdateScore inside our UIManager script

Now we can alter our previous AddScore function inside the Player script:

New AddScore function

Giving our Enemies some Score

We might want different enemies to have different Score values, so we’ll update the Enemy script to include a Point Value variable.

Now each Enemy is worth 10 points!

And we’ll create a reference to our Player:

_player reference in the Enemy Script

Now we can reference our AddScore() function from our Player script. This makes the most sense when the Enemy is destroyed by our Laser:

Adding our _enemyPointValue to our _totalScore

If all was done correctly, we should be updating our Score in real-time! When the Enemy is destroyed, it calls the AddScore function from the Player, which in-turn calls the UpdateScore function in the UIManager. Good stuff!

Proper Scoring system!

In the next article we’ll be continuing in the UI section, by displaying our Player’s lives!

--

--

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles