1
00:00:03,960 --> 00:00:09,390
All game is now at the stage where the player can click on one of the answer buttons and be told whether

2
00:00:09,390 --> 00:00:11,470
they got the answer right or wrong.

3
00:00:11,490 --> 00:00:16,980
But we do have a little bit of a bug here because even though we've now selected an answer, there's

4
00:00:16,980 --> 00:00:22,260
nothing to stop us from selecting a new answer and potentially changing our result.

5
00:00:22,740 --> 00:00:26,450
And the thing that's controlling this is on the answer buttons.

6
00:00:26,460 --> 00:00:32,850
So if we go ahead and click on one of our answer buttons and scroll down to look at the button component,

7
00:00:32,880 --> 00:00:38,520
we have this interactive toggle flag when it's checked, that means the button can still be clicked

8
00:00:38,520 --> 00:00:42,500
on and if we disable it, then the button will no longer work.

9
00:00:42,510 --> 00:00:47,970
So we want to look at doing this programmatically and disabling the buttons when an answer has been

10
00:00:47,970 --> 00:00:48,750
selected.

11
00:00:49,200 --> 00:00:51,810
So let's jump over into our quiz script.

12
00:00:51,810 --> 00:00:58,080
And before we go ahead and do this, I do want to do a little bit of refactoring and tidy up our start

13
00:00:58,080 --> 00:00:58,740
method.

14
00:00:59,070 --> 00:01:04,769
This was some of the first code we wrote, and whilst it's fine to stay in the start method for now,

15
00:01:04,769 --> 00:01:10,110
it really is a good idea to tidy that up and put it in its own method so that we can reuse it later.

16
00:01:10,110 --> 00:01:15,330
And also just to wrap it up with a nice name so that we can remember exactly what it does.

17
00:01:16,730 --> 00:01:21,730
So let's scroll down and make some space for it and keep all of our code tidy.

18
00:01:21,740 --> 00:01:27,500
If we hover over by the line numbers, we can actually collapse down sections of our code to hide it

19
00:01:27,500 --> 00:01:28,070
away.

20
00:01:28,190 --> 00:01:33,860
So underneath our public method, we're going to create a new private method, and this doesn't have

21
00:01:33,860 --> 00:01:36,530
to return anything so we can make it a void.

22
00:01:36,530 --> 00:01:39,470
And we're going to call this method display question.

23
00:01:41,100 --> 00:01:47,220
Because if you remember this code in start at the moment, pretty much just displays the questions and

24
00:01:47,220 --> 00:01:48,730
answers on the canvas.

25
00:01:48,750 --> 00:01:54,780
So let's then go ahead and cut and paste all of our code into that new method, and then we're going

26
00:01:54,780 --> 00:01:58,050
to go and call this method from within start.

27
00:01:58,770 --> 00:02:04,110
So not a drastic change to our code or performance there, but whenever we do things like this, it

28
00:02:04,110 --> 00:02:07,450
is a good idea to just save the script and pop back into unity.

29
00:02:07,470 --> 00:02:09,820
To double check that we haven't broken anything.

30
00:02:09,840 --> 00:02:13,810
So if you want to err on the side of caution, then go ahead and do that now.

31
00:02:13,830 --> 00:02:15,330
I'm not going to worry too much.

32
00:02:15,330 --> 00:02:17,820
I'm fairly confident that I've got it right.

33
00:02:18,090 --> 00:02:21,060
And now we can start thinking about our buttons.

34
00:02:21,510 --> 00:02:24,960
So let's have a quick look at the game flow that we're after.

35
00:02:25,080 --> 00:02:28,680
First of all, we're going to display a new question to the player.

36
00:02:28,950 --> 00:02:33,120
And in doing so, we're going to turn on all of the answer buttons.

37
00:02:33,300 --> 00:02:40,410
Then once the question has been answered, we want to turn all the buttons off and then loop back around

38
00:02:40,410 --> 00:02:42,810
to potentially display a new question.

39
00:02:43,050 --> 00:02:49,740
So for turning the buttons on and off, we could have two separate methods for this one to set them

40
00:02:49,740 --> 00:02:52,410
all to true and one to set them all to false.

41
00:02:52,410 --> 00:02:57,660
But because the logic for both is incredibly similar, we're going to look at killing two birds with

42
00:02:57,660 --> 00:03:00,480
one stone and just writing a single method.

43
00:03:00,930 --> 00:03:08,460
So back into our quiz script and at the bottom underneath display questions, I'm going to create another

44
00:03:08,460 --> 00:03:12,510
new method that will set the button state to whatever we pass in.

45
00:03:12,690 --> 00:03:19,320
This method doesn't need to return anything so we can make that void and we'll call it set button state.

46
00:03:21,280 --> 00:03:23,230
Then to tell it what state we want.

47
00:03:23,230 --> 00:03:27,190
We're going to pass in a bill and call this the state.

48
00:03:27,190 --> 00:03:29,470
So that's just going to be true or false.

49
00:03:30,610 --> 00:03:34,180
This method is then going to have to loop through all of the answer buttons.

50
00:03:34,180 --> 00:03:38,560
So very similar to what we're doing when we're displaying a question up here.

51
00:03:38,920 --> 00:03:43,840
So let's say that for int i equals zero.

52
00:03:44,350 --> 00:03:52,480
Our condition will then be when I is less than the answer button's length and our iterator will be going

53
00:03:52,480 --> 00:03:53,220
up by I.

54
00:03:53,260 --> 00:03:54,160
Plus plus.

55
00:03:56,060 --> 00:04:02,540
Next, we need to find a reference to the button component on our answer button game object so we can

56
00:04:02,540 --> 00:04:10,250
have a type button and we'll call this the button and we're going to set this equal to the answer buttons.

57
00:04:10,250 --> 00:04:15,320
At index, I get component of type button.

58
00:04:16,730 --> 00:04:22,880
With that reference to the button in hand, we can then go ahead and say button dot intractable.

59
00:04:23,720 --> 00:04:25,190
I are not is intractable.

60
00:04:25,220 --> 00:04:31,700
That actually tells you the state that it's currently in we want dot intractable.

61
00:04:32,550 --> 00:04:35,030
And that will allow you to set that flag.

62
00:04:35,040 --> 00:04:36,290
So do be careful.

63
00:04:36,300 --> 00:04:41,400
There are a few methods dotted around unity that are like that that you may think do one thing, but

64
00:04:41,400 --> 00:04:45,330
actually do something else, but with the correct one in hand.

65
00:04:45,330 --> 00:04:51,660
Now the button interact table, we're going to set this equal to the state that we're passing into our

66
00:04:51,660 --> 00:04:52,230
method.

67
00:04:52,230 --> 00:04:55,770
So that will either set that flag to either true or false.

68
00:04:56,340 --> 00:04:58,770
Next, we need to call this from somewhere.

69
00:04:58,770 --> 00:05:04,890
And we said we want to set this button state when we get a new question and put it on the canvas and

70
00:05:04,890 --> 00:05:10,410
also when the answer has been selected, well, let's do the answer selected version first.

71
00:05:10,410 --> 00:05:14,400
So let's scroll up to where our on answer selected method is.

72
00:05:14,430 --> 00:05:21,090
It's currently rolled up, so we just need to go next to the number line here and unroll that down and

73
00:05:21,090 --> 00:05:22,920
then down at the bottom of that method.

74
00:05:22,920 --> 00:05:29,430
Once we've done all of our other work, let's go ahead and call that set button state method and we're

75
00:05:29,430 --> 00:05:34,620
going to pass in false because we want to disable the buttons at this stage.

76
00:05:35,600 --> 00:05:41,680
So that takes care of disabling the buttons, but we are going to need to re-enable them at some point.

77
00:05:41,690 --> 00:05:45,230
So let's have a think about where we're going to do that now.

78
00:05:45,230 --> 00:05:50,820
We could put it in display question because that's roughly when we're going to want to do it.

79
00:05:50,840 --> 00:05:57,050
However, later on, we are going to be doing some other stuff around getting a new question as well.

80
00:05:57,050 --> 00:06:02,780
So rather than making this method do more than one thing, let's create another method above it.

81
00:06:02,870 --> 00:06:05,750
And this again is going to be a void.

82
00:06:06,050 --> 00:06:09,290
And we're going to call this one get next question.

83
00:06:11,270 --> 00:06:17,120
Then inside this method, we're going to set the button state back to true so the player can click on

84
00:06:17,120 --> 00:06:19,220
the buttons again when we get a new question.

85
00:06:19,400 --> 00:06:22,250
And we're also going to display the question.

86
00:06:23,030 --> 00:06:27,410
So with that set up, let's save our script and jump back into unity.

87
00:06:28,680 --> 00:06:33,630
Now, if we select an answer and then try and click on a different answer, we'll notice that all of

88
00:06:33,630 --> 00:06:35,370
the buttons have been disabled.

89
00:06:35,640 --> 00:06:39,090
So that's the disabling of the buttons now working.

90
00:06:39,090 --> 00:06:44,190
And unfortunately, at the moment, we're not going to be able to see the re enabling of the buttons

91
00:06:44,190 --> 00:06:47,760
because we do only have one question in our quiz.

92
00:06:48,120 --> 00:06:51,420
So for now, we're just going to have to kind of hope that that works.

93
00:06:51,420 --> 00:06:54,540
And if we have any problems, we can always come back to it later.

94
00:06:54,540 --> 00:07:00,510
So as a developer, it may just be worth making a note to say that that feature is currently untested.

95
00:07:00,930 --> 00:07:02,880
And talking of untested features.

96
00:07:02,880 --> 00:07:08,430
There is one more thing that I want to do in this script, because it's going to set us up nicely for

97
00:07:08,430 --> 00:07:15,690
a later lecture, and it's also a very good excuse to have a challenge because in addition to resetting

98
00:07:15,690 --> 00:07:22,380
the button state, when a new question is presented, we're also going to want to reset the button sprites

99
00:07:22,380 --> 00:07:27,570
so that we don't continue to highlight the old answers that we had from the previous question.

100
00:07:27,870 --> 00:07:33,960
So when we get our next question, which again, we're not currently calling at the moment, we're going

101
00:07:33,960 --> 00:07:35,970
to set our button state to true.

102
00:07:36,450 --> 00:07:39,930
We're then going to set the default button sprites.

103
00:07:40,890 --> 00:07:46,080
And that's a method that we're about to write and then we can go ahead and display our new question.

104
00:07:46,830 --> 00:07:49,620
So let's go ahead and write this method.

105
00:07:50,520 --> 00:07:53,790
And we'll do that down at the bottom underneath set button state.

106
00:07:54,300 --> 00:07:57,840
We can say void set, default button sprites.

107
00:07:58,050 --> 00:08:01,170
And writing this is going to be your challenge.

108
00:08:01,950 --> 00:08:06,090
I want you to write this set default button sprouts method.

109
00:08:06,420 --> 00:08:11,520
Now, using the code that we've already written in this section, you should be able to piece together

110
00:08:11,520 --> 00:08:13,500
how to do this on your own.

111
00:08:13,500 --> 00:08:18,780
But if you would like to hang around for some extra logic, then the logic for this method is going

112
00:08:18,780 --> 00:08:24,090
to first be to loop through all of our answer buttons, which we've done many times now.

113
00:08:24,090 --> 00:08:30,060
We then want to get the image component off of each one of those buttons and change the sprite back

114
00:08:30,060 --> 00:08:31,530
to our default sprite.

115
00:08:31,950 --> 00:08:34,770
So pause the video now and give that one a go.

116
00:08:39,990 --> 00:08:40,440
Okay.

117
00:08:40,440 --> 00:08:41,370
Welcome back.

118
00:08:41,400 --> 00:08:46,620
So hopefully you got on really well with that challenge because it is something we've done a few times

119
00:08:46,620 --> 00:08:47,110
now.

120
00:08:47,130 --> 00:08:53,910
So for this, we're going to need a for loop to loop through our buttons so we can say four int equals

121
00:08:53,910 --> 00:08:54,570
zero.

122
00:08:55,140 --> 00:09:02,250
Our condition will be while I is less than the answer buttons length and we're going to iterate by one

123
00:09:02,250 --> 00:09:03,030
every loop.

124
00:09:03,660 --> 00:09:11,880
We then want to store a reference to an image component so we can say image button image equals the

125
00:09:11,880 --> 00:09:12,840
answer buttons.

126
00:09:12,840 --> 00:09:24,060
At index I get component of type image and then we can set the button image sprite to be equal to the

127
00:09:24,060 --> 00:09:25,890
default answer sprite.

128
00:09:26,190 --> 00:09:32,190
So congratulations if you manage to complete that challenge and piece all of that together and to just

129
00:09:32,190 --> 00:09:36,780
double check that this is all working, let's actually go up to start.

130
00:09:36,780 --> 00:09:45,060
And rather than calling display question here, let's call get next question because at the moment this

131
00:09:45,060 --> 00:09:46,560
should be fine for testing.

132
00:09:46,920 --> 00:09:51,690
Let's then jump back into unity and just go ahead and kind of break things just a little bit.

133
00:09:51,690 --> 00:09:55,740
So on our buttons we're going to turn them off by default for testing.

134
00:09:56,220 --> 00:10:02,070
And I'm also going to change the sprites to the answer sprite by default.

135
00:10:02,340 --> 00:10:07,500
Now what this will mean is that if our methods that we've written have worked, then when we press play,

136
00:10:07,530 --> 00:10:13,260
these buttons will change back to our blue default sprite and the buttons will be clickable.

137
00:10:13,930 --> 00:10:16,420
So let's go ahead and hit play and test this out.

138
00:10:17,440 --> 00:10:19,660
And indeed everything has worked.

139
00:10:19,900 --> 00:10:24,970
And now that everything's working for a single question, I think it's time to start adding our other

140
00:10:24,970 --> 00:10:28,750
questions to our game and looking at how to switch between them.

141
00:10:28,750 --> 00:10:31,870
So that's all for now, and I'll see you in the next lecture.

