1
00:00:03,610 --> 00:00:08,740
In this lecture, we're going to be adding a scorekeeper to our game to track how the player is doing

2
00:00:08,740 --> 00:00:10,390
as they progress through our quiz.

3
00:00:10,870 --> 00:00:16,120
Okay, so the first thing we want to do before we worry about any coding is to just place some score

4
00:00:16,120 --> 00:00:17,890
text on our canvas.

5
00:00:17,950 --> 00:00:22,620
So let's go ahead and open up the pre visualization canvas to see where we want this to go.

6
00:00:22,630 --> 00:00:25,270
And I've got mine up in the top left corner.

7
00:00:25,870 --> 00:00:32,380
On our quiz canvas lets right click, go down to UI and then up to our text mesh pro text element.

8
00:00:32,860 --> 00:00:37,300
We can go ahead and drag that into position and roughly resize it.

9
00:00:38,050 --> 00:00:41,570
And let's also go and rename our component in the hierarchy.

10
00:00:41,590 --> 00:00:44,260
So let's call this the score text.

11
00:00:44,830 --> 00:00:51,160
Let's change the dummy text itself to something like score at, say, 100%.

12
00:00:52,000 --> 00:00:59,950
And then let's resize it to around 48 points, change the alignment options and then go ahead and hide

13
00:00:59,950 --> 00:01:03,490
the pre visualization canvas to see everything together.

14
00:01:03,760 --> 00:01:05,710
That looks okay to me for now.

15
00:01:05,740 --> 00:01:10,870
So now let's go ahead and create our scorekeeper script down in our scripts folder.

16
00:01:10,870 --> 00:01:16,480
Lets right click and create a new C-sharp script and call this one the scorekeeper.

17
00:01:17,300 --> 00:01:19,330
Now let's add this script to an object.

18
00:01:19,330 --> 00:01:25,030
In our scene, just like our timer, we could reasonably put it anywhere that feels sensible.

19
00:01:25,030 --> 00:01:30,580
And because we've got our timer on a separate object, let's go ahead and create a new empty object

20
00:01:30,580 --> 00:01:32,530
and call this one the scorekeeper.

21
00:01:33,430 --> 00:01:37,540
It's reset the transform and drag over our scorekeeper script.

22
00:01:37,780 --> 00:01:39,640
And now let's open that script up.

23
00:01:40,000 --> 00:01:45,180
Now, when it comes to scoring in a game, there are many different options that you might take.

24
00:01:45,190 --> 00:01:50,080
You might want to add a certain amount of points to each question that gets answered.

25
00:01:50,080 --> 00:01:54,760
But as you may have already guessed, I'm going to go with a percentage based system.

26
00:01:54,760 --> 00:01:58,060
And for this system, we're going to need to know two things.

27
00:01:58,060 --> 00:02:02,800
We're going to need to know how many correct answers the player has got, and we're going to need to

28
00:02:02,800 --> 00:02:05,850
know how many questions the player has seen to that point.

29
00:02:05,860 --> 00:02:08,889
So let's create two variables to store these values.

30
00:02:09,070 --> 00:02:11,230
These are both going to be integers.

31
00:02:11,230 --> 00:02:18,100
So we will call the first one correct answers and we'll default that to zero to start with and we'll

32
00:02:18,100 --> 00:02:20,290
call the second one questions.

33
00:02:20,290 --> 00:02:21,070
Same.

34
00:02:21,640 --> 00:02:24,460
And again, we'll default that 1 to 0 to start with.

35
00:02:24,730 --> 00:02:30,490
Now for the access level of these two variables, we are going to need to see them in other scripts.

36
00:02:30,490 --> 00:02:35,290
But rather than making them public and allowing them to be edited at will, we're going to use some

37
00:02:35,290 --> 00:02:41,230
getters to protect access, but we're also going to use something called a setter method to help control

38
00:02:41,230 --> 00:02:44,200
which values get passed into these variables.

39
00:02:44,410 --> 00:02:48,100
So let's go ahead and delete, start an update because we're not going to need them.

40
00:02:48,460 --> 00:02:51,880
And let's start with the getter which we've seen in previous lectures.

41
00:02:52,030 --> 00:02:54,040
Let's set up the correct answers first.

42
00:02:54,040 --> 00:02:58,900
So we're going to have a public int called get correct answers.

43
00:03:00,610 --> 00:03:05,350
And we may actually not need this particular getter, but it's worth having in there for completeness,

44
00:03:05,350 --> 00:03:09,160
just in case we change our minds or change how our game works later on.

45
00:03:09,550 --> 00:03:13,090
And this method is just going to return the correct answers.

46
00:03:13,740 --> 00:03:20,210
Next, we're going to need a setter method which will control which values we can pass in to these variables.

47
00:03:20,220 --> 00:03:25,950
And for this particular game, I'm going to use a setter that just increments the values by one every

48
00:03:25,950 --> 00:03:26,580
time.

49
00:03:26,700 --> 00:03:31,710
So let's create another public method, and this one isn't going to return anything so we can make it

50
00:03:31,710 --> 00:03:32,350
void.

51
00:03:32,370 --> 00:03:36,720
And let's go ahead and call this one increment correct answers.

52
00:03:39,930 --> 00:03:43,230
And in here, all we're going to do is just say correct answers.

53
00:03:43,230 --> 00:03:44,240
Plus, plus.

54
00:03:44,250 --> 00:03:49,770
So every time we call this setter method, we'll just increase the correct answer count by one.

55
00:03:50,250 --> 00:03:55,860
So with our getter and setter method set up for our correct answers, we now need to do the same thing

56
00:03:55,860 --> 00:03:56,940
for our questions.

57
00:03:56,940 --> 00:03:58,050
Same variable.

58
00:03:58,080 --> 00:04:00,420
And that is going to be your challenge.

59
00:04:00,660 --> 00:04:03,660
I want you to create the getter method for questions.

60
00:04:03,660 --> 00:04:09,660
Same, and I want you to create the setter method for question scene, the increments, the stored value

61
00:04:09,660 --> 00:04:11,600
by one every time it's called.

62
00:04:11,610 --> 00:04:14,160
So pause the video now and give that a go.

63
00:04:19,459 --> 00:04:19,790
Okay.

64
00:04:19,820 --> 00:04:20,760
Welcome back.

65
00:04:20,779 --> 00:04:27,530
So hopefully fairly simple for our get a method we're going to have a public int called get question

66
00:04:27,530 --> 00:04:33,380
seen and all this needs to do is return question.

67
00:04:33,380 --> 00:04:41,900
Same then for our sets of method, we're going to have a public void increment questions same.

68
00:04:42,950 --> 00:04:47,270
And this is just going to take questions same and add one.

69
00:04:47,990 --> 00:04:53,570
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

70
00:04:53,570 --> 00:04:55,190
to calculate the score.

71
00:04:55,940 --> 00:04:56,930
Now for my game.

72
00:04:56,930 --> 00:05:02,060
I want my percentage values to be rounded to the nearest whole number, so I don't want any trailing

73
00:05:02,060 --> 00:05:04,010
decimals left in my answer.

74
00:05:04,430 --> 00:05:08,780
So with that in mind, let's create the method that's going to calculate our score.

75
00:05:09,110 --> 00:05:15,380
We're going to want a public method which returns an integer value, and we'll call this one calculate

76
00:05:15,380 --> 00:05:16,070
score.

77
00:05:18,420 --> 00:05:24,780
Now, if we think back to math class to get a percentage value, we're going to want to return the correct

78
00:05:24,780 --> 00:05:31,290
answers divided by the questions seen multiplied by 100.

79
00:05:31,620 --> 00:05:37,590
However, because our correct answers and questions C are both integers, unfortunately, it's not going

80
00:05:37,590 --> 00:05:44,430
to be quite that simple because whenever we do integer division, the answer will also be an integer.

81
00:05:44,460 --> 00:05:51,600
So for example, if we answered three questions correctly and we'd seen four questions, we would expect

82
00:05:51,600 --> 00:05:55,470
this to come out at 0.75 or 75%.

83
00:05:55,740 --> 00:06:01,380
However, with the integer division that we've got at the moment, this would actually return zero.

84
00:06:01,710 --> 00:06:08,610
Now, the way we go ahead and fix it is to cast one of these integer values into a float.

85
00:06:09,060 --> 00:06:11,760
So let's do that on our questions scene.

86
00:06:11,760 --> 00:06:18,600
And the way we cast a variable is to place some parentheses before it and then tell it the type of variable

87
00:06:18,600 --> 00:06:19,830
we wanted to be.

88
00:06:20,010 --> 00:06:23,850
So in this case, we want our questions seen to be a float.

89
00:06:24,240 --> 00:06:28,740
In our example, this would now treat our four as a 4.0.

90
00:06:28,860 --> 00:06:33,030
And now, rather than doing integer division, we're now doing floating point division.

91
00:06:33,030 --> 00:06:35,910
And so the answer will come back as a float.

92
00:06:36,300 --> 00:06:41,640
Now we'll notice this has given us a big red squiggly line, and if we hover over it, it will tell

93
00:06:41,640 --> 00:06:45,630
us that we can't implicitly convert type float to an int.

94
00:06:45,630 --> 00:06:50,820
And that's because we're trying to return a float where we're supposed to be returning an integer.

95
00:06:51,210 --> 00:06:56,610
Now, we could go ahead and wrap this entire expression in parentheses and then cast it back to being

96
00:06:56,610 --> 00:06:57,450
an integer.

97
00:06:57,450 --> 00:07:01,470
But this can get a little messy and I also want to do some rounding on this.

98
00:07:01,470 --> 00:07:07,800
So instead, let's take advantage of the math f class, which has a lot of mathematical operations available

99
00:07:07,800 --> 00:07:13,700
to us and the one we want for our particular problem is going to be round to int.

100
00:07:13,710 --> 00:07:18,750
So this is going to take a floating point number in and then round it to the nearest integer.

101
00:07:18,960 --> 00:07:25,380
So let's wrap our expression in the parentheses and our red squiggly goes away and this should now work

102
00:07:25,380 --> 00:07:26,130
correctly.

103
00:07:26,400 --> 00:07:29,100
Let's get rid of our little example comment at the end.

104
00:07:29,100 --> 00:07:34,950
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.

105
00:07:35,680 --> 00:07:39,150
Let's start by adding a couple of new variables to our list here.

106
00:07:39,160 --> 00:07:46,540
So underneath timer, let's add a new header called scoring and then we're going to need a serialized

107
00:07:46,540 --> 00:07:51,910
field of type text mesh, pro-EU Gooey and we'll call this the score text.

108
00:07:51,910 --> 00:07:54,430
So that's the text on our UI canvas.

109
00:07:54,730 --> 00:07:57,430
And then we're going to need a reference to our scorekeeper.

110
00:07:57,430 --> 00:08:00,580
So let's have scorekeeper and we'll call it the scorekeeper.

111
00:08:02,160 --> 00:08:04,380
In start let's find that scorekeeper.

112
00:08:04,380 --> 00:08:09,750
So let's say scorekeeper equals find object of type scorekeeper.

113
00:08:11,810 --> 00:08:15,430
Now let's look at incrementing our correct answers and questions.

114
00:08:15,430 --> 00:08:16,030
Singh.

115
00:08:16,540 --> 00:08:21,850
So if we scroll down to our display answers method, because that's where we were checking whether we

116
00:08:21,850 --> 00:08:27,670
got the right answer or not in our first if block if we did get the answer correct, let's go ahead

117
00:08:27,670 --> 00:08:29,420
and say scorekeeper.

118
00:08:29,450 --> 00:08:35,049
Dot increment correct answers and then down and get next question.

119
00:08:35,049 --> 00:08:41,620
So when we get the question initially, we'll set our scorekeeper dot increment question Singh.

120
00:08:41,950 --> 00:08:45,300
So that takes care of the numbers being incremented.

121
00:08:45,310 --> 00:08:52,240
And to show our score on the UI itself, let's scroll back up to on answer selected I think would be

122
00:08:52,240 --> 00:08:53,260
a good place to do it.

123
00:08:53,470 --> 00:09:02,740
And we'll set the score text text equal to score plus the scorekeeper dot calculate score.

124
00:09:03,340 --> 00:09:08,620
And to cap that score off, let's also add a percentage sign at the end.

125
00:09:09,660 --> 00:09:15,420
So let's go ahead and save our script and jump back over into Unity on our quiz canvas.

126
00:09:15,420 --> 00:09:16,590
Let's hook things up.

127
00:09:16,590 --> 00:09:19,710
So we need to drag over our score text into the field.

128
00:09:20,430 --> 00:09:23,550
And now let's go ahead and hit play and check that everything works.

129
00:09:24,980 --> 00:09:29,540
So our score is starting off at 100, which we may want to change later on.

130
00:09:29,690 --> 00:09:33,160
But if we get a question wrong, it should go down to 0%.

131
00:09:34,360 --> 00:09:40,180
And if we go ahead and get a question right, then we'll see that our score jumps up to 50%.

132
00:09:40,600 --> 00:09:43,900
So our scoring system seems to be working correctly.

133
00:09:44,200 --> 00:09:48,970
And as we continue to get questions right or wrong, our score will update accordingly.

134
00:09:49,360 --> 00:09:55,090
So with that, we now have a fully functioning scoring system and it's arguably flexible enough you

135
00:09:55,090 --> 00:09:58,210
could potentially reuse it for other games in the future.

136
00:09:58,750 --> 00:10:04,300
Now there's one more piece of UI functionality that I want to add to my quiz, and that is to tell the

137
00:10:04,300 --> 00:10:08,170
player how far through the quiz they are when they answer a question.

138
00:10:08,290 --> 00:10:10,500
So that's what we'll do in the next lecture.

139
00:10:10,510 --> 00:10:11,920
So I'll see you there.

