Keeping Score In My Game

PieceofPhie
3 min readApr 2, 2021

Today I added a simple score system and User interface to display it.

We get five points every time we destroy an enemy ship and display the total score on the top right of the screen.

Our new UI consists of a game object named UIManager with a script that handles our UI named ,’UI Manager’. The chided canvas and Text that we will be using to display our score.

The text’s anchor is anchored to the top right and Canvas’s UI scale mode set to ,’Scale With Screen Size. This will allow the UI to scale appropriately to any screen.

Note: The UI will sometimes adjust to the screen when adjusting it within unity, However it'll scale during game play.

UI Manager Script

Our UI Manager is simple. It only has one function that has one int parameter that is assigned to a local variable named CurrentScore. CurrentScore is then displayed within Score.text by concatenating (Joining a string and other data together) String data,’Score’ and integer data,’CurrentScore using the + symbol.

Score.Text only takes String Data. Without concatenating CurrentScore to a string we would have to convert our Int by using .ToString() function.

So Score.text = CurrentScore.Tostring();

Next we’ll be working within our player script.

We’ll be using the player script to store our total points and calling UpdateScore() function from our UIManager script.

Our script first attempts to popular our UIManager script reference ,’PUI’ by using a local game object variable named,’FindPlayerUI’. FindPlayerUI will search for the UIManager gameobject and assign the UIManager gameobject to itself.

If the Player is present in the scene then there must be a UIManager to accompany it.

I then use a IF statement to check if the FindPlayerUI is null. If its true, then we assign PUI to FindPlayerUI’s UIManager component. If it returns false then a debug.log will be printed into console and scripts will not work properly.

We have access to the UIManager script throughout our code since our reference is declared globally.

Our AddPoints() function has one int parameter that is added to our private float variable Score. Score is then Passed to UpdateScore() int parameter.

So we add the points and update the text on screen accordingly.

Lastly, our enemy script.

Enemy Script

our objective here is that we want to call our AddPoints() function when the enemy has made contact with a laser.

I have declared a new private variable named ,’PointValue’ which can be adjusted via inspector.

We will be passing PointValue int into AddPoints Parameter. Using PointValue int will allow me to have different enemies that will award the player with varying points in the future.

Our Enemy script has an identical setup to our player script above. It has a globally declared reference to our player script and will search for the player game object. Then it will null check and so on so forth.

Running the same setup allows me to access two different functions for two different instances of collision. Originally only accessing it through as if statement that tested for the Player tag.

I can now call the Damage() Function when the player has collided and the AddPoints() function when collided with a laser.

--

--