1
00:00:03,730 --> 00:00:09,010
Okay, everyone, it's been a long time coming, but we're finally going to be adding our other questions

2
00:00:09,010 --> 00:00:10,050
to our quiz.

3
00:00:10,060 --> 00:00:13,810
And to do this, we're going to be using something called a list.

4
00:00:14,140 --> 00:00:15,610
So what is a list?

5
00:00:15,640 --> 00:00:20,910
Well, they're kind of like arrays, which we've already seen when we were storing our buttons.

6
00:00:20,920 --> 00:00:26,770
So if we consider the similarities to an array, a list is still a grouping of multiple variables of

7
00:00:26,770 --> 00:00:28,060
the same type.

8
00:00:28,150 --> 00:00:31,990
Each item stored within our list is still called an element.

9
00:00:32,049 --> 00:00:35,350
Each element can be accessed by its index number.

10
00:00:35,710 --> 00:00:38,560
And counting again starts at zero.

11
00:00:38,950 --> 00:00:43,490
So there are the similarities, and you may currently be thinking that they're virtually identical.

12
00:00:43,510 --> 00:00:50,650
But the big difference between a list and an array is that lists are mutable, meaning that we can change

13
00:00:50,650 --> 00:00:51,640
their size.

14
00:00:51,880 --> 00:00:56,790
So with an array, once it's created, we can't change the size by the list.

15
00:00:56,800 --> 00:00:59,770
We can add and remove items as much as we like.

16
00:01:00,160 --> 00:01:03,360
So let's look at the syntax of how to create a list.

17
00:01:03,370 --> 00:01:08,920
If we first consider the syntax for an array, then we can create a new array like this.

18
00:01:09,070 --> 00:01:12,460
But for a list, the syntax is slightly different.

19
00:01:13,030 --> 00:01:18,430
First, we're going to specify that we want a list, and then inside the angled brackets or chevrons,

20
00:01:18,430 --> 00:01:21,760
we're going to tell the list what type it's going to be storing.

21
00:01:21,760 --> 00:01:24,970
So in this example, we're storing a list of integers.

22
00:01:25,270 --> 00:01:31,360
We then have the name of our variable, which in this case is odd numbers, and then we can initialize

23
00:01:31,360 --> 00:01:35,620
this list by saying new list of type integer.

24
00:01:36,370 --> 00:01:39,010
So that's how we're going to be creating a new list.

25
00:01:39,010 --> 00:01:42,250
But now let's look at some useful methods that are going to come in handy.

26
00:01:42,790 --> 00:01:48,670
First of all, if we want to check the item count within our list, we can use list count.

27
00:01:48,670 --> 00:01:53,680
So this is very similar to the array length that we were using before.

28
00:01:54,280 --> 00:02:00,640
Next, if we want to check that an item exists within our list, we can use list DOT contains and then

29
00:02:00,640 --> 00:02:02,810
we can pass in the thing we're looking for.

30
00:02:02,830 --> 00:02:08,770
So with the example of storing our odd numbers, this would ask whether the number three currently exists

31
00:02:08,770 --> 00:02:09,729
in our list or not.

32
00:02:10,240 --> 00:02:15,490
If we'd like to add a new item, we can use list, add and then tell it what we want to add.

33
00:02:15,490 --> 00:02:21,670
So in this case, the number three, we can remove an item with list, remove and again pass in what

34
00:02:21,670 --> 00:02:27,190
we want to remove if it exists, if we don't know a specific value that's contained in our list and

35
00:02:27,190 --> 00:02:34,030
we just want to remove the item at a given index we could use list remove at and then passing the index

36
00:02:34,030 --> 00:02:35,050
that we're interested in.

37
00:02:35,050 --> 00:02:39,640
So in this case, remove at zero would remove the first element in our list.

38
00:02:40,060 --> 00:02:45,880
And finally, another good one to remember is list dot clear, which will completely empty our list.

39
00:02:46,150 --> 00:02:51,760
So with explanations out of the way, let's go ahead and create our list for our questions.

40
00:02:52,300 --> 00:02:57,640
However, before we go ahead and do that, there's one thing I want to change about our current code,

41
00:02:57,640 --> 00:03:01,750
and that's our current serialised field for our current question.

42
00:03:02,050 --> 00:03:06,850
Once we've created our list, we're no longer going to need this serialized in the inspector.

43
00:03:06,850 --> 00:03:11,560
So let's go ahead and clear things up by just removing that serialized filled attribute.

44
00:03:11,950 --> 00:03:17,230
And the other thing I want to do is actually rename this variable because our list is going to have

45
00:03:17,230 --> 00:03:18,730
a very similar name.

46
00:03:18,730 --> 00:03:24,580
And to avoid errors and confusion as we continue to code, let's go ahead and rename this by pressing

47
00:03:24,580 --> 00:03:29,380
F two and instead of question, let's call this the current question.

48
00:03:30,210 --> 00:03:35,520
With that renamed that should have replaced any instance of our old question with current question.

49
00:03:35,520 --> 00:03:39,390
So if we have a quick scroll down, we can see that in display answer.

50
00:03:39,390 --> 00:03:42,090
It's now looking at the current question instead.

51
00:03:43,180 --> 00:03:43,870
And in display.

52
00:03:43,870 --> 00:03:50,320
QUESTION We're also looking at the current question so minor code fixing aside, let's go back up to

53
00:03:50,320 --> 00:03:54,070
our variables and let's go ahead and create our list.

54
00:03:54,220 --> 00:03:57,430
We're going to want to drag over our questions in the inspector.

55
00:03:57,430 --> 00:04:05,080
So let's make this a serialized field and we can create a new list by saying list of type question.

56
00:04:05,080 --> 00:04:11,170
So, so our question, script table objects and we'll give this variable the name questions, which

57
00:04:11,170 --> 00:04:15,550
is why we wanted to change our old question variable to current question.

58
00:04:16,060 --> 00:04:20,320
Now, because this is serialized, we can go ahead and leave this as it is.

59
00:04:20,320 --> 00:04:25,660
But if you want some added protection in case you remove the serialized field later on in your journey,

60
00:04:25,660 --> 00:04:31,630
we can instantiate this list by writing equals new list of type question.

61
00:04:31,640 --> 00:04:38,170
So, so with our list set up and once we've dragged the questions over in the Inspector, we're going

62
00:04:38,170 --> 00:04:42,970
to need to get questions from this list and make them the current question.

63
00:04:43,420 --> 00:04:46,810
So let's scroll down to our get next question method.

64
00:04:47,410 --> 00:04:52,390
And now when we get a new question, we're instead going to want to get the question from our list.

65
00:04:52,720 --> 00:04:58,810
Now, there are many ways we may decide to get these questions from our list, but to add a bit of randomness

66
00:04:58,810 --> 00:05:02,830
to our game, I'm going to be pulling a random question out of our list.

67
00:05:02,830 --> 00:05:07,060
Whenever we need to get a new one, we're going to do this in a new method.

68
00:05:07,180 --> 00:05:12,820
So once we've set the button state and the button sprites, but before we display our question, let's

69
00:05:12,820 --> 00:05:16,480
go ahead and write a new method called Get Random Question.

70
00:05:17,650 --> 00:05:19,590
Then we're going to need to write this method.

71
00:05:19,600 --> 00:05:25,090
So underneath our next question, let's create a void, get random question method.

72
00:05:25,690 --> 00:05:27,850
Let's start by getting that random number.

73
00:05:27,850 --> 00:05:32,560
So we're going to have a temporary variable of type int called index.

74
00:05:32,860 --> 00:05:42,220
And this is going to be equal to random range with a minimum of zero and a maximum of our questions

75
00:05:42,640 --> 00:05:43,390
count.

76
00:05:43,390 --> 00:05:49,630
So this is going to return a random number between element zero of our list and the last element in

77
00:05:49,630 --> 00:05:50,260
our list.

78
00:05:50,530 --> 00:05:56,770
With that index, we can then say that our current question is going to be equal to the question in

79
00:05:56,770 --> 00:06:03,340
our list at that index, and then we want to go ahead and remove that question from the list so we can

80
00:06:03,340 --> 00:06:08,380
say questions, remove the current question.

81
00:06:09,520 --> 00:06:15,310
Now, this should all be fine, but whenever I remove an item from a list, it's always good to just

82
00:06:15,310 --> 00:06:21,220
double check that it actually exists to avoid any potential errors cropping up and just to err on the

83
00:06:21,220 --> 00:06:22,210
side of caution.

84
00:06:22,600 --> 00:06:27,220
This shouldn't strictly be necessary for our game because it is very small and we're not going to be

85
00:06:27,220 --> 00:06:29,710
doing much else with our questions list.

86
00:06:29,710 --> 00:06:34,330
But in larger projects, when we start moving things around and we have a lot of things talking to each

87
00:06:34,330 --> 00:06:40,210
other, it's always good to make sure this check happens so that we don't accidentally run into a problem

88
00:06:40,210 --> 00:06:42,310
that may cause our game to throw an error.

89
00:06:42,580 --> 00:06:51,490
And we can check to see that our current question exists in our list by saying If questions DOT contains

90
00:06:52,480 --> 00:06:57,340
the current question, then we want to go ahead and remove it.

91
00:06:57,610 --> 00:07:03,220
Now, with that set, let's go ahead and save our script and jump over into Unity to test it all out.

92
00:07:03,940 --> 00:07:09,670
If we look in the inspector under our questions header, we now have a list of questions that we can

93
00:07:09,670 --> 00:07:10,150
use.

94
00:07:10,360 --> 00:07:16,240
So let's lock the inspector so that we can drag our questions over and over to the questions folder

95
00:07:16,240 --> 00:07:23,110
where we stored all of our script objects and then select all of them and drag them over into our list.

96
00:07:23,560 --> 00:07:28,270
Now for me, that's put them in reverse order, but because I'm pulling them at random, it doesn't

97
00:07:28,270 --> 00:07:29,620
really matter too much.

98
00:07:29,620 --> 00:07:34,600
If you want to change the order of any of the elements, you can just click and drag to move them up

99
00:07:34,600 --> 00:07:35,350
or down.

100
00:07:36,880 --> 00:07:41,980
And if you want to add or remove any other element, then we can either use the plus and minus buttons

101
00:07:41,980 --> 00:07:45,880
at the bottom or we can change the size of our list with this input field.

102
00:07:46,210 --> 00:07:50,200
So questions all set up, we should now be picking questions at random.

103
00:07:50,350 --> 00:07:52,780
So let's go ahead and hit play to test this out.

104
00:07:53,410 --> 00:07:57,700
And before we go ahead and answer a question, I'm going to pause the game and click back on my quiz

105
00:07:57,700 --> 00:08:04,030
canvas so I can see the questions that are in our list, and we'll notice that two of them have actually

106
00:08:04,030 --> 00:08:06,250
been removed rather than just one.

107
00:08:06,640 --> 00:08:12,670
Now, the good news is the questions were randomly selected, but why did we remove two of them?

108
00:08:12,970 --> 00:08:17,560
Well, there's a very logical explanation for this, which we'll fix in just a moment.

109
00:08:17,560 --> 00:08:22,420
But for now, let's just do a little bit more testing to make sure that it doesn't happen for every

110
00:08:22,420 --> 00:08:23,440
single question.

111
00:08:24,420 --> 00:08:27,510
So let's unpause our game and wait for the timer to run down.

112
00:08:27,870 --> 00:08:32,159
And for all subsequent questions, it does look like it's just removing the one.

113
00:08:32,340 --> 00:08:38,070
So if we quickly skip through the rest of our quiz, well, notice that once we've answered the final

114
00:08:38,070 --> 00:08:44,730
question and our list is empty, then if we check the console, we're getting an error saying that the

115
00:08:44,730 --> 00:08:50,790
index was out of range, i.e. we're trying to pull an item out of a list when the list is empty.

116
00:08:51,090 --> 00:08:53,130
So two bugs to fix there.

117
00:08:53,130 --> 00:08:55,500
So let's jump back over into our script.

118
00:08:55,890 --> 00:09:02,400
And the first bug of removing two questions at once is incredibly simple to fix because if we head up

119
00:09:02,400 --> 00:09:08,700
to our start method, we're currently getting the next question in START and we don't actually want

120
00:09:08,700 --> 00:09:09,990
to be doing that anymore.

121
00:09:09,990 --> 00:09:14,310
So let's delete that line and also our commented outline as well.

122
00:09:14,490 --> 00:09:19,170
And that should fix the problem with two questions being selected on the first run through.

123
00:09:19,910 --> 00:09:25,490
And then to fix the error that comes up when we've run out of questions in our list, let's head down

124
00:09:25,490 --> 00:09:29,930
to our get next question method and we're going to put in a check here.

125
00:09:30,290 --> 00:09:33,260
And I think this would make a very good challenge for you.

126
00:09:33,830 --> 00:09:39,620
I want you to go ahead and modify our get next question method to check whether there are still questions

127
00:09:39,620 --> 00:09:43,820
left in our list before we go ahead and execute those other four methods.

128
00:09:43,820 --> 00:09:46,670
So pause the video now and give that one a go.

129
00:09:52,020 --> 00:09:52,410
Okay.

130
00:09:52,410 --> 00:09:53,310
Welcome back.

131
00:09:53,340 --> 00:09:55,620
So hopefully you got on well with that challenge.

132
00:09:55,620 --> 00:10:03,840
And the solution to this problem is going to be to use an if statement and ask if the questions dot

133
00:10:03,840 --> 00:10:06,600
count is greater than zero.

134
00:10:07,500 --> 00:10:11,070
Then we want to go ahead and run these four methods.

135
00:10:12,180 --> 00:10:18,050
So let's go ahead and test this out to see if everything's worked through the magic of video editing.

136
00:10:18,060 --> 00:10:21,630
We can find that both of these bugs have now been fixed.

137
00:10:21,630 --> 00:10:23,070
So congratulations.

138
00:10:23,070 --> 00:10:29,040
We now have a game with multiple questions and at this stage you can feel free to add as many other

139
00:10:29,040 --> 00:10:30,960
questions to your game as you like.

140
00:10:31,410 --> 00:10:36,030
Now, the next thing we're going to want to look at in our game is to improve the UI a little bit because

141
00:10:36,030 --> 00:10:41,340
we now have multiple questions, but the player has no idea how far through the game they are.

142
00:10:41,370 --> 00:10:45,300
But that's a topic for another lecture, so I'll see you there.

