In this lecture, we're going to be adding a scorekeeper to our game to track how the player is doing
as they progress through our quiz.
Okay, so the first thing we want to do before we worry about any coding is to just place some score
text on our canvas.
So let's go ahead and open up the pre visualization canvas to see where we want this to go.
And I've got mine up in the top left corner.
On our quiz canvas lets right click, go down to UI and then up to our text mesh pro text element.
We can go ahead and drag that into position and roughly resize it.
And let's also go and rename our component in the hierarchy.
So let's call this the score text.
Let's change the dummy text itself to something like score at, say, 100%.
And then let's resize it to around 48 points, change the alignment options and then go ahead and hide
the pre visualization canvas to see everything together.
That looks okay to me for now.
So now let's go ahead and create our scorekeeper script down in our scripts folder.
Lets right click and create a new C-sharp script and call this one the scorekeeper.
Now let's add this script to an object.
In our scene, just like our timer, we could reasonably put it anywhere that feels sensible.
And because we've got our timer on a separate object, let's go ahead and create a new empty object
and call this one the scorekeeper.
It's reset the transform and drag over our scorekeeper script.
And now let's open that script up.
Now, when it comes to scoring in a game, there are many different options that you might take.
You might want to add a certain amount of points to each question that gets answered.
But as you may have already guessed, I'm going to go with a percentage based system.
And for this system, we're going to need to know two things.
We're going to need to know how many correct answers the player has got, and we're going to need to
know how many questions the player has seen to that point.
So let's create two variables to store these values.
These are both going to be integers.
So we will call the first one correct answers and we'll default that to zero to start with and we'll
call the second one questions.
Same.
And again, we'll default that 1 to 0 to start with.
Now for the access level of these two variables, we are going to need to see them in other scripts.
But rather than making them public and allowing them to be edited at will, we're going to use some
getters to protect access, but we're also going to use something called a setter method to help control
which values get passed into these variables.
So let's go ahead and delete, start an update because we're not going to need them.
And let's start with the getter which we've seen in previous lectures.
Let's set up the correct answers first.
So we're going to have a public int called get correct answers.
And we may actually not need this particular getter, but it's worth having in there for completeness,
just in case we change our minds or change how our game works later on.
And this method is just going to return the correct answers.
Next, we're going to need a setter method which will control which values we can pass in to these variables.
And for this particular game, I'm going to use a setter that just increments the values by one every
time.
So let's create another public method, and this one isn't going to return anything so we can make it
void.
And let's go ahead and call this one increment correct answers.
And in here, all we're going to do is just say correct answers.
Plus, plus.
So every time we call this setter method, we'll just increase the correct answer count by one.
So with our getter and setter method set up for our correct answers, we now need to do the same thing
for our questions.
Same variable.
And that is going to be your challenge.
I want you to create the getter method for questions.
Same, and I want you to create the setter method for question scene, the increments, the stored value
by one every time it's called.
So pause the video now and give that a go.
Okay.
Welcome back.
So hopefully fairly simple for our get a method we're going to have a public int called get question
seen and all this needs to do is return question.
Same then for our sets of method, we're going to have a public void increment questions same.
And this is just going to take questions same and add one.
So with our getters and setters all set up, it's time to do a bit of math and work out how we're going
to calculate the score.
Now for my game.
I want my percentage values to be rounded to the nearest whole number, so I don't want any trailing
decimals left in my answer.
So with that in mind, let's create the method that's going to calculate our score.
We're going to want a public method which returns an integer value, and we'll call this one calculate
score.
Now, if we think back to math class to get a percentage value, we're going to want to return the correct
answers divided by the questions seen multiplied by 100.
However, because our correct answers and questions C are both integers, unfortunately, it's not going
to be quite that simple because whenever we do integer division, the answer will also be an integer.
So for example, if we answered three questions correctly and we'd seen four questions, we would expect
this to come out at 0.75 or 75%.
However, with the integer division that we've got at the moment, this would actually return zero.
Now, the way we go ahead and fix it is to cast one of these integer values into a float.
So let's do that on our questions scene.
And the way we cast a variable is to place some parentheses before it and then tell it the type of variable
we wanted to be.
So in this case, we want our questions seen to be a float.
In our example, this would now treat our four as a 4.0.
And now, rather than doing integer division, we're now doing floating point division.
And so the answer will come back as a float.
Now we'll notice this has given us a big red squiggly line, and if we hover over it, it will tell
us that we can't implicitly convert type float to an int.
And that's because we're trying to return a float where we're supposed to be returning an integer.
Now, we could go ahead and wrap this entire expression in parentheses and then cast it back to being
an integer.
But this can get a little messy and I also want to do some rounding on this.
So instead, let's take advantage of the math f class, which has a lot of mathematical operations available
to us and the one we want for our particular problem is going to be round to int.
So this is going to take a floating point number in and then round it to the nearest integer.
So let's wrap our expression in the parentheses and our red squiggly goes away and this should now work
correctly.
Let's get rid of our little example comment at the end.
Save our script and now let's go ahead and hook it all up in our quiz script so we can see it in action.
Let's start by adding a couple of new variables to our list here.
So underneath timer, let's add a new header called scoring and then we're going to need a serialized
field of type text mesh, pro-EU Gooey and we'll call this the score text.
So that's the text on our UI canvas.
And then we're going to need a reference to our scorekeeper.
So let's have scorekeeper and we'll call it the scorekeeper.
In start let's find that scorekeeper.
So let's say scorekeeper equals find object of type scorekeeper.
Now let's look at incrementing our correct answers and questions.
Singh.
So if we scroll down to our display answers method, because that's where we were checking whether we
got the right answer or not in our first if block if we did get the answer correct, let's go ahead
and say scorekeeper.
Dot increment correct answers and then down and get next question.
So when we get the question initially, we'll set our scorekeeper dot increment question Singh.
So that takes care of the numbers being incremented.
And to show our score on the UI itself, let's scroll back up to on answer selected I think would be
a good place to do it.
And we'll set the score text text equal to score plus the scorekeeper dot calculate score.
And to cap that score off, let's also add a percentage sign at the end.
So let's go ahead and save our script and jump back over into Unity on our quiz canvas.
Let's hook things up.
So we need to drag over our score text into the field.
And now let's go ahead and hit play and check that everything works.
So our score is starting off at 100, which we may want to change later on.
But if we get a question wrong, it should go down to 0%.
And if we go ahead and get a question right, then we'll see that our score jumps up to 50%.
So our scoring system seems to be working correctly.
And as we continue to get questions right or wrong, our score will update accordingly.
So with that, we now have a fully functioning scoring system and it's arguably flexible enough you
could potentially reuse it for other games in the future.
Now there's one more piece of UI functionality that I want to add to my quiz, and that is to tell the
player how far through the quiz they are when they answer a question.
So that's what we'll do in the next lecture.
So I'll see you there.