1
00:00:03,660 --> 00:00:08,940
In this lecture, we're going to finish off our script to objects so that they also hold the answers

2
00:00:08,940 --> 00:00:10,110
to our questions.

3
00:00:10,110 --> 00:00:13,710
And in the process, we're going to learn a little bit more about arrays.

4
00:00:14,960 --> 00:00:20,750
So here we are in one of our scripts or objects which is currently only holding the question text.

5
00:00:20,750 --> 00:00:25,400
So let's have a think about how we're going to store the answers to our quiz.

6
00:00:25,640 --> 00:00:30,310
Well, on our canvas we have four buttons, so one for each answer.

7
00:00:30,320 --> 00:00:35,300
So your first instinct might be to store each answer as its own unique string.

8
00:00:35,600 --> 00:00:42,020
This is a perfectly fine approach, but it is a little bit cumbersome and not very flexible.

9
00:00:42,020 --> 00:00:47,390
So rather than doing this way, what we're instead going to do is use something called an array.

10
00:00:48,290 --> 00:00:50,450
So what is an array?

11
00:00:50,690 --> 00:00:56,310
Well, you can think of them as a grouping of multiple variables of the same type.

12
00:00:56,330 --> 00:01:02,720
So if we think about this in terms of our answers, rather than having four unique string variables,

13
00:01:02,720 --> 00:01:05,000
we can have a single array of strings.

14
00:01:05,030 --> 00:01:11,840
Now, when we group variables into an array, each item that we store in that array is called an element,

15
00:01:11,840 --> 00:01:16,430
and each element can be accessed within the array by its index number.

16
00:01:16,430 --> 00:01:21,230
So they're very easy to work with, especially when we start looking at looping through them.

17
00:01:21,260 --> 00:01:27,530
Now one thing to note about arrays and a lot of things in computers, in fact, is that counting starts

18
00:01:27,530 --> 00:01:28,480
at zero.

19
00:01:28,490 --> 00:01:32,750
So the first element in an array is element zero.

20
00:01:32,780 --> 00:01:38,090
And this is initially quite confusing if you're not used to this zero indexing that computers use,

21
00:01:38,090 --> 00:01:43,100
but with a little bit of time and a little bit of practice, this will soon become second nature to

22
00:01:43,100 --> 00:01:43,490
you.

23
00:01:43,850 --> 00:01:47,480
So let's look at an example of what an array might look like.

24
00:01:47,570 --> 00:01:54,140
Well, here we have an array of odd numbered integers, and specifically we've got the first five odd

25
00:01:54,140 --> 00:01:54,830
numbers.

26
00:01:55,010 --> 00:01:58,880
Now, just like any other variable, we have declared the type at the front.

27
00:01:58,880 --> 00:02:01,180
So this is storing integer values.

28
00:02:01,190 --> 00:02:07,940
But notice these brackets after the type declaration, these brackets indicate that we're now dealing

29
00:02:07,940 --> 00:02:11,420
with an array of integers rather than just a single integer.

30
00:02:11,720 --> 00:02:14,750
We have our name of our variable just like any other.

31
00:02:14,750 --> 00:02:20,390
So we've called this one the odd numbers and we've initialized it with the first five odd numbers.

32
00:02:20,390 --> 00:02:26,960
So notice that we've used these braces on either side and then we have five elements inside this.

33
00:02:27,110 --> 00:02:33,080
But because we start counting at zero, each element within that would have an index of zero, one,

34
00:02:33,080 --> 00:02:35,470
two, three and four.

35
00:02:35,480 --> 00:02:41,000
And again, this can be a little bit confusing if you're not used to this zero indexing, but please

36
00:02:41,000 --> 00:02:45,380
do trust me on this, that the more that you work with them, the easier this does become.

37
00:02:45,710 --> 00:02:51,740
Now, this isn't the only way to initialize an array, so we don't necessarily always know what values

38
00:02:51,740 --> 00:02:53,600
we want to place in the array.

39
00:02:53,750 --> 00:02:57,710
And when that happens, what we can do is we can initialize it like this.

40
00:02:58,100 --> 00:02:59,900
So the front is exactly the same.

41
00:02:59,900 --> 00:03:06,380
We've still got an array of integers called odd numbers, but now we're initializing it as a new array

42
00:03:06,380 --> 00:03:09,950
of integers with a size of five.

43
00:03:09,950 --> 00:03:15,740
So we're saying that this array will have five slots, if you like, five elements contained within

44
00:03:15,740 --> 00:03:16,130
it.

45
00:03:16,130 --> 00:03:21,920
And then later on we might access one of those elements by its index number and then change that.

46
00:03:21,920 --> 00:03:24,440
The same we might change a regular old integer.

47
00:03:25,520 --> 00:03:31,010
So let's jump over into our script object now and see these arrays in action.

48
00:03:31,620 --> 00:03:36,880
Now the answers to all questions are generally going to be strings and so make it into an array.

49
00:03:36,900 --> 00:03:38,670
I just need to open some brackets.

50
00:03:38,910 --> 00:03:41,460
I'm then going to give my variable a name.

51
00:03:41,460 --> 00:03:43,740
So let's call this the answers.

52
00:03:43,890 --> 00:03:49,260
And because I don't necessarily know what the answers are going to be ahead of time, all I know is

53
00:03:49,260 --> 00:03:52,010
that each question will have four answers.

54
00:03:52,020 --> 00:03:59,370
I'm going to initialize my array by saying that it's equal to a new string array.

55
00:03:59,370 --> 00:04:04,560
So with the brackets open there and within these brackets, I'm going to put the number four.

56
00:04:04,770 --> 00:04:07,740
So there's going to be four elements to my array.

57
00:04:08,430 --> 00:04:13,740
The next step now we've set up the core of our variable is to think about the access level that it needs.

58
00:04:14,010 --> 00:04:20,070
So just like our question, we're going to want to be able to change them in the inspector, but we

59
00:04:20,070 --> 00:04:23,910
don't want to be able to change them from outside the script object.

60
00:04:23,910 --> 00:04:29,400
So for this, I think a serialized field is again a very good choice for the access.

61
00:04:29,610 --> 00:04:35,490
And if we save our script table object and jump back into unity, we'll see that we now have this rolled

62
00:04:35,490 --> 00:04:43,140
up option for answers in our script or objects so we can unroll this and we have four elements and notice

63
00:04:43,140 --> 00:04:50,160
the element start counting from zero up to three and we can go ahead and then fill in the potential

64
00:04:50,160 --> 00:04:51,990
answers to our question.

65
00:04:52,500 --> 00:04:55,590
Now this question has some answers attached to it.

66
00:04:55,590 --> 00:04:59,010
We now need to know which one is the correct one.

67
00:04:59,040 --> 00:05:04,740
So now we're going to need another variable attached to our script to object to tell us which element

68
00:05:04,740 --> 00:05:07,320
of this array holds the correct answer.

69
00:05:07,650 --> 00:05:10,230
So back over into our script will object.

70
00:05:10,230 --> 00:05:16,650
Let's again create another new variable and this time it's going to hold the index number for our answer.

71
00:05:16,650 --> 00:05:22,980
So before you just follow along with me, see if you can write this variable for yourself and this isn't

72
00:05:22,980 --> 00:05:23,700
a full challenge.

73
00:05:23,700 --> 00:05:26,940
So I'll just give you a moment to think before carrying on.

74
00:05:30,220 --> 00:05:30,730
Okay.

75
00:05:30,730 --> 00:05:37,300
So this is going to be a integer type because we're asking for an index number for our array and I'm

76
00:05:37,300 --> 00:05:40,510
going to name this the correct answer index.

77
00:05:42,900 --> 00:05:48,570
Now we need a way of setting this in the inspector and we still don't want this to be public to other

78
00:05:48,570 --> 00:05:49,860
scripts in our game.

79
00:05:49,860 --> 00:05:57,090
So let's again use a serialized field and that will now give us a way of storing the index to the correct

80
00:05:57,090 --> 00:05:57,840
answer.

81
00:05:57,870 --> 00:06:04,410
So if we start, say, the number three as our correct answer index, this would relate to the fourth

82
00:06:04,410 --> 00:06:10,020
element of our answers array because again, remember that this starts counting at zero.

83
00:06:10,590 --> 00:06:14,810
Next, we want to go ahead and finish off our script object.

84
00:06:14,820 --> 00:06:21,240
So just like with our public getter method for our question, we're going to need a getter method for

85
00:06:21,240 --> 00:06:27,990
the correct answer index and another getter method to get an answer at a specific index.

86
00:06:28,350 --> 00:06:31,740
And I think that brings us nicely on to your challenge.

87
00:06:31,950 --> 00:06:35,220
I want you to create two more getter methods.

88
00:06:35,520 --> 00:06:39,840
The first one is going to be called Get Correct Answer Index.

89
00:06:39,960 --> 00:06:45,780
So think about the access level of the method, the return type of the method and what variable it's

90
00:06:45,780 --> 00:06:47,280
actually going to return.

91
00:06:47,550 --> 00:06:52,590
And the second one, which is a little bit more complicated but not by much, is going to be called

92
00:06:52,590 --> 00:06:54,000
get answer.

93
00:06:54,000 --> 00:06:59,340
And I say this one is a little bit more complex because this one is also going to accept a parameter

94
00:06:59,340 --> 00:07:04,890
of type int, which is going to be called index, so that when you use this method from outside the

95
00:07:04,890 --> 00:07:10,920
script, you can pass in the index for the answer that you want and get that particular answer out of

96
00:07:10,920 --> 00:07:12,150
our answers array.

97
00:07:12,420 --> 00:07:15,810
So pause the video now and give that one a go.

98
00:07:21,480 --> 00:07:21,990
Okay.

99
00:07:21,990 --> 00:07:22,820
Welcome back.

100
00:07:22,830 --> 00:07:27,690
So hopefully you got on well with both of those, but don't worry too much if you struggled with the

101
00:07:27,690 --> 00:07:28,470
second one.

102
00:07:28,470 --> 00:07:34,350
Let's go through both of them now for the first one to return the correct answer index.

103
00:07:34,380 --> 00:07:40,410
Our getter methods are always going to be public and in this case it's going to return an integer.

104
00:07:40,590 --> 00:07:44,760
And we said that we were going to call this one get correct answer index.

105
00:07:47,180 --> 00:07:52,010
I know this one is going to do is return the correct answer index.

106
00:07:52,370 --> 00:07:56,290
So congratulations if you manage to do that first one without any problems.

107
00:07:56,300 --> 00:07:58,360
And now let's look at the second one.

108
00:07:58,370 --> 00:08:00,200
And I'm actually going to put this one just above.

109
00:08:00,200 --> 00:08:02,090
It doesn't really matter where you place it.

110
00:08:02,090 --> 00:08:05,360
So if you put it below, then that's absolutely fine as well.

111
00:08:05,630 --> 00:08:11,270
And again, it's a getter method, so it's going to have to be public so other scripts can use it and

112
00:08:11,270 --> 00:08:13,130
it's going to return a string.

113
00:08:13,340 --> 00:08:16,520
And this is just a single string, so not an array of strings.

114
00:08:17,090 --> 00:08:19,940
And we said this was going to be called get answer.

115
00:08:21,430 --> 00:08:29,290
We also said that this was going to accept a parameter of type int called index and for this one we

116
00:08:29,290 --> 00:08:35,500
want to return the answers and the index that we specified.

117
00:08:35,830 --> 00:08:42,880
So what this method is doing is it's passing in an index that we want to use and then just returning

118
00:08:42,880 --> 00:08:45,520
the answer at that given index.

119
00:08:45,880 --> 00:08:53,820
And if we go ahead and save our script and head back over into unity, we now have our complete script

120
00:08:53,830 --> 00:08:54,550
for object.

121
00:08:54,550 --> 00:08:56,290
We have space for the question.

122
00:08:56,440 --> 00:09:03,320
We have space for all four answers, and we can also specify the index for the correct answer.

123
00:09:03,340 --> 00:09:08,650
So in this case, the correct index is zero, but we can place any number in there that we like.

124
00:09:08,650 --> 00:09:12,100
So it might be that we need the correct answer index to be two.

125
00:09:12,280 --> 00:09:18,700
Now, one thing to be very careful of going forward is that this correct answer index is currently unconstrained.

126
00:09:18,700 --> 00:09:25,630
So we can have the correct answer of -56 or the correct answer of 54, and we should probably try and

127
00:09:25,630 --> 00:09:26,410
prevent that.

128
00:09:26,410 --> 00:09:32,440
So the designer doesn't accidentally put in something crazy, but for now, we'll leave it as it is,

129
00:09:32,620 --> 00:09:35,590
and we may come to fix that in a later lecture.

130
00:09:35,920 --> 00:09:40,450
For now, though, I think we've spent long enough on our script to object, and it's about time we

131
00:09:40,450 --> 00:09:45,040
started actually displaying our questions on the canvas that we worked so hard on.

132
00:09:45,220 --> 00:09:50,260
So that would be the subject of the next lecture where we finally start displaying all of this information

133
00:09:50,260 --> 00:09:51,400
on the canvas.

134
00:09:51,400 --> 00:09:52,840
So I'll see you there.

