GameDev : Score System implementation (Part I)!

Matteo Lo Piccolo
5 min readJun 13, 2021

--

Now we have our UI Manager with the Text component, and it’s ok.
We tested it with “Score: 50”, but we need the Score change every time we kill an Enemy.
In Player script, at the bootm

we have Pseudo Code.
Let’s turn it into real code, creating the AddScore () method

I’m continue with easy implementation, so inside it we set Score += 10;
Nothing crazy, we only add 10 points every time.

Now we have AddScore method, where we call it?
Inside Enemy script we had another piece of Pseudo Code here, in the If Statement

delete Pseudo Code and add
player.AddScore()

TIP 1 : I don’t remember if I changed this part of code without explanation, but can be possible for some of you this does not work.
The reason is simple, and is about the SCOPE OF VARIABLE.
If we look at my script, the variable “player” is not inside the first IF, but it is at the TOP OF THE TRIGGER METHOD.
This means that every method I create inside it has ACCESS TO THIS VARIABLE, but if I have the variable only inside the first If, outside of the simple one it doesn’t exist!
So, if you have that problem, this can help you fix it.

TIP 2 : the best thing to do, when we call GetComponent method, is nearly always cash it into Start. This because GetComponent is really “expansive operation”. We are not yet in that phase where we have memory management problems, but is good to know that THIS CAN BE A PROBLEM.

So, to make things better, let’s move the reference into Start.

Simply cut and paste with Ctrl+C and Ctrl+V.
Now we have some errors, and it’s perfectly fine.

Rename it to
player = GameObject.Find(“Player”).GetComponent<Player>();

We have an error, because we don’t have player’s reference.
Now we use simple shortcut : move the cursor near player, Alt+Enter, create Enemy.player field and press Enter again

To make things better, CTRL+R+R on player, and rename it to _player

This is perfectly fine, but we need another check.
We AddScore to the Player here…

…but if Player is dead?
Technically this is not a problem, because if we are dead, we can’t kill nothing, so why this is a problem?
Well, there is always a 1% of cases where something happen because we don’t make a simple check.

Imagine this: we have a life, we fire a laser but we die. The Laser crosses the screen, hits the Enemy, is called Addscore and … we are NULL, because we are dead …
How many times can this happen? Maybe 3, maybe 4, maybe never.
But we MUST ALWAYS DO CHECKS FOR EVERY CASE

And we make a check if the Player is not NULL.

Press Play

In the Player Inspector we see the Score go up 10 every time we kill Enemy.
Now we need to connect the AddScore method to UI to make it change.

But before, we need a little change here.
The “problem” is that because we hardcoding 10, if we want make a change for some reason , we have to go INSIDE SCRIPT, and this is terrible design choice.

Let’s try to make this system to work better.
Everything works good, so we have the right logic, we simply need to set the things in modular way.

For example, we have AddScore, and we add 10 points

Now, if we pass a PARAMETER, an int value to ADD TO THE SCORE?

points is a simple variable

the problem now is that every time call this method, it ask us an int.
In fact, now we have an error.

If we press CTRL+SHIFT+B, the compiler try to “compile” all the solution, so we can check if everything works, or maybe with some change we have break something.
And in my case happen this

If we double click on error

takes us directly to the “wrong” line of code.

The problem is : we have AddScore with int paramenter, so we MUST PASS a parameter to make it work.

This is a better way to make things, but we have yet the same problem, if we want to change the number, we need to go inside script.
How can we do?

Well, AddScore ask us a int parameter, and if we create a NEW VARIABLE inside Enemy and we call it points

And we pass THIS VARIABLE INSIDE ADDSCORE

Now we have a variable “points” that WE CAN CHANGE IN THE INSPECTOR IF WE WANT.
And the AddScore works great because inside we pass an int value.

In the next part, set the user interface to read AddScore ()!

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet