Before we start worrying about the look of our future UI, let's first implement a scoring system so
the player can see how well they're doing in the game and also make a couple of tweaks to our health
script so that we can show the player their current health.
And because I think you're completely up to the task now, this is going to be a rather large challenge
for you.
So I want you to go ahead and write a public getter method in our health script to return the current
health of the player.
And then I want you to write a scorekeeper script which will attach to an object in the hierarchy.
This script will have a private variable to store the current score, and because this variable will
be private, we're going to need a public getter method to return this current score so we can send
that off to our UI.
Later on, we're going to need a public method to modify that score, and we're also going to want a
public method to reset that score.
And finally, for this challenge, we want to increase the score whenever an enemy is destroyed.
So that, again, will be in our health script.
So a pretty large challenge here, but pause the video and give it your best shot.
Okay.
Welcome back.
And a very big congratulations if you managed to do every single part of that challenge, if you didn't
know, not to worry, we're going to go through it now.
So the first part of this challenge was to make a public get a method for our health.
So if we jump into our health script, we have our private health here.
So let's just go and make a new public method and I'm going to put that underneath my on trigger enter.
So I've got all of my unity built in methods together, my private methods together, and now our public
methods.
This is going to have to return an integer and we're going to call it Get Health.
And this is a very simple getter, so it's just going to return our health.
So that's the first part of the challenge done.
And now if we head into unity, we're going to create our scorekeeper.
Let's right click in the hierarchy, create a new empty game object and reset its position.
Let's call this game object the scorekeeper.
And we're going to create a new C-sharp script to attach to this called The Scorekeeper.
Let's drag our script onto our object and then open that up for this.
We're not going to need our start and our update methods, but we are going to need a private variable
of type INT to hold our score.
Because this is private, we're going to need a public getter for this.
So we'll have a public int get score and this will just return our score with that set.
Let's give ourselves a way to modify this score.
So this time we're going to have a public void called Modify Score, and this is going to accept a parameter
of type int called value.
So how much we want to modify this score by and then to modify our score or we need to say is score
plus equals our value.
Now we could get away with leaving this method as it is, but I'm going to do a little bit of extra
work to clamp my score so it doesn't go below zero.
So for this we can say math f clamp.
We want to clamp our score.
We don't want it to go below zero and we don't care how high it goes.
So we can say int dot max value.
Finally, for this method, we wanted a way to reset our score.
So setting it back to zero.
So again we'll create a public method of type void and call this one reset score.
And this method just has to set our score equal to zero.
So congratulations if you manage to write all of that yourself.
And the last step of your challenge was to increment the score whenever we killed an enemy.
So if we jump back over to our health script right at the top, we're going to need a reference to our
scorekeeper.
So we'll call this the scorekeeper in Awake.
We then need to find our scorekeeper so we can use find object of type scorekeeper.
And because we're using this whole script on both our player and our enemies, we want a way of separating
these outs.
We're not adding a score when the player dies.
So let's write at the top at a new serialised field of type bull and we'll call.
This is Player.
This will also have a use later on, so it's quite nice that we're adding it now.
Finally, we're going to need to give these things a skull so we could potentially use the base health
for this.
But I think it would be quite nice if we had a independent variable for the score alone.
So this is going to be type int and we'll call it the score and we can set this equal to maybe 50 to
mirror our default health.
But having these variables separate will allow us to potentially have a very low health enemy that's
very hard to hit and therefore be worth a lot more points.
So with our variables all set, let's head down to our take damage method, which is where our things
with health are being killed off.
And because we're going to eventually have a lot more stuff going on in this if statement, it would
make sense to refactor this into a new method.
So let's call this method die for now.
A little bit morbid, but it does the job.
And then we can create our die method where we can destroy our game object.
Before we destroy it though, let's add our score to our scorekeeper so we can say that if this is not
the player, then we want to take our scorekeeper and modify the score passing in the score for this
object.
And before we go and test this in unity, let's jump back to our scorekeeper very quickly, because
at the moment, until we've got our UI in, we have no way of checking with this scoring system is working.
So when we modify our score, let's just add a nice debug log to tell us our score.
So let's save up our scripts and jump into unity to test.
Now let's head over to our prefabs so we can look at our enemy and our player.
If we look at our player first and head down to its health script, we want to say that it is the player
so it won't recognize any score.
And because the score is mostly irrelevant, let's just set that to zero just in case we forget to check
our is player flag.
Then over on our enemy we should have our is player flag set to false and we want to give it some sort
of score and I'm going to leave it as 54 now.
So if we click on our console so we can see our score being logged out here and go ahead and hit play.
And now if we try and kill some enemies, we will get our score being locked out.
So congratulations on creating that scorekeeper.
And if you manage to do it all from the challenge and give yourself a big pat on the back because that
wasn't a small piece of work with all of that set up.
Now we're in a very good place to start adding our UI.
We'll do that in the next lecture, so I'll see you there.