1
00:00:03,860 --> 00:00:09,200
In this lecture, we're going to start creating the questions for our quiz using script table objects.

2
00:00:09,770 --> 00:00:14,150
Before we go ahead and start doing that, though, there's a little bit of tidy up that we need to do

3
00:00:14,150 --> 00:00:19,640
in our project because our hierarchy is starting to get a little bit messy and so is our asset folder.

4
00:00:19,880 --> 00:00:23,030
So in our asset folder, let's clean this up first.

5
00:00:23,030 --> 00:00:28,460
And the first thing I want to do is just store away our font assets in the text message folder.

6
00:00:28,670 --> 00:00:35,210
So let's open that up and we're going to drag the font file into the fonts folder and we're going to

7
00:00:35,210 --> 00:00:40,660
drag the font asset into resources, fonts and materials.

8
00:00:41,600 --> 00:00:46,490
We've also got this button prefab, but this button doesn't really tell us what it does.

9
00:00:46,490 --> 00:00:52,310
So let's give that a more descriptive name by just pressing F two and we'll call this the answer button

10
00:00:52,310 --> 00:00:53,030
instead.

11
00:00:54,780 --> 00:00:56,910
I will also create a new folder for that.

12
00:00:56,910 --> 00:01:03,660
So right click create folder and we'll call this prefabs and drag that over into there.

13
00:01:03,930 --> 00:01:06,060
That tidies up our asset folder.

14
00:01:06,210 --> 00:01:09,210
So over in our hierarchy, let's do some work in here.

15
00:01:09,210 --> 00:01:14,190
And I'm not going to worry too much about the pre visualization canvas or the background canvas, but

16
00:01:14,190 --> 00:01:17,700
the children of our quiz canvas could certainly do with some better naming.

17
00:01:17,700 --> 00:01:22,380
So I'm going to rename my text object to the question text.

18
00:01:22,380 --> 00:01:27,600
And under our answer button group, I'm just going to rename our buttons to answer button.

19
00:01:28,710 --> 00:01:32,190
And I'm also going to give it an index that starts at zero.

20
00:01:32,400 --> 00:01:36,990
And since these are all prefabs, I can just go ahead and delete these and then duplicate again.

21
00:01:36,990 --> 00:01:40,140
So we have answer button zero through to answer button three.

22
00:01:40,500 --> 00:01:41,770
With all that set up.

23
00:01:41,790 --> 00:01:46,620
Finally, let's just add a couple of new blank folders to our assets ready for the stuff that we're

24
00:01:46,620 --> 00:01:47,550
about to do.

25
00:01:47,580 --> 00:01:52,800
So the first thing I'm going to do is create a new folder called Script, which will hold all of our

26
00:01:52,800 --> 00:01:54,060
C sharp scripts.

27
00:01:54,480 --> 00:01:56,580
And I'm also going to create a folder.

28
00:01:57,530 --> 00:02:01,070
Called questions which will hold all of our script objects.

29
00:02:01,370 --> 00:02:03,190
Okay, so project all set up.

30
00:02:03,200 --> 00:02:08,479
Let's actually talk about what script will objects are and how they're going to help us with our game.

31
00:02:09,190 --> 00:02:11,710
So what is a script or object?

32
00:02:11,740 --> 00:02:16,770
Well, it's really just a data container, so a way of storing data for our gang.

33
00:02:16,780 --> 00:02:20,800
And they're useful because they keep this data out of our script.

34
00:02:20,800 --> 00:02:25,750
And this helps us to save memory by storing all of that data in a single place that our scripts can

35
00:02:25,750 --> 00:02:26,500
reference.

36
00:02:26,530 --> 00:02:31,870
They also don't need to be attached to game objects like normal mono behavior scripts, and this makes

37
00:02:31,870 --> 00:02:34,360
them very lightweight and convenient to use.

38
00:02:34,600 --> 00:02:40,090
And probably the best way to think of scripts for objects is as a kind of template, so we can use them

39
00:02:40,090 --> 00:02:41,410
as a kind of blueprint.

40
00:02:41,410 --> 00:02:43,690
And this helps with data consistency.

41
00:02:44,020 --> 00:02:49,810
Some examples of where script objects can be useful is for things like storing weapon statistics in

42
00:02:49,810 --> 00:02:54,460
an RPG, so things like the damage and critical charts and things like that.

43
00:02:54,700 --> 00:02:59,950
We could also use them to store card data in a collectible card game, and for our game we're going

44
00:02:59,950 --> 00:03:02,500
to be using them to store question data.

45
00:03:02,500 --> 00:03:08,950
So things like the question, text itself, all of the possible answers to that question and also a

46
00:03:08,950 --> 00:03:10,840
reference to the correct answer.

47
00:03:11,520 --> 00:03:16,860
Now I mentioned that the script objects kind of separate to the mono behavior scripts that you're familiar

48
00:03:16,860 --> 00:03:17,320
with.

49
00:03:17,340 --> 00:03:23,280
So on the one side, we have our code, which might be a mono behavior called quizzes.

50
00:03:23,580 --> 00:03:28,290
And in the script, we might have various methods for displaying the question on screen and checking

51
00:03:28,290 --> 00:03:29,160
the answer.

52
00:03:29,190 --> 00:03:33,150
And we could store all of the question data in this script as well.

53
00:03:33,360 --> 00:03:39,960
However, if we rip this data out of our script and store them in script objects, we could then reference

54
00:03:39,960 --> 00:03:44,420
one of those script will objects when we need to get the question data that we're after.

55
00:03:44,430 --> 00:03:49,620
And this will then just look at the data in the script object without actually copying it anywhere.

56
00:03:50,570 --> 00:03:52,810
Now jumping back over into unity.

57
00:03:52,820 --> 00:03:57,230
Let's actually create some script objects in our scripts folder.

58
00:03:57,260 --> 00:04:03,770
Let's right click and go up to create new C-sharp script and we're going to call this the question.

59
00:04:03,830 --> 00:04:07,730
So for script object, let's go ahead and open this up.

60
00:04:08,580 --> 00:04:14,160
And the first thing I want to do in here is to actually delete the start and update method, because

61
00:04:14,160 --> 00:04:18,540
not only are we not going to need it, we're not actually going to be able to use them with scripts

62
00:04:18,550 --> 00:04:19,470
or objects.

63
00:04:19,470 --> 00:04:24,900
Because if we look at the class declaration at the top here, we can see that it inherits from mono

64
00:04:24,900 --> 00:04:25,800
behavior.

65
00:04:25,800 --> 00:04:30,030
And this will be the case for every script that you've written so far in this course.

66
00:04:30,060 --> 00:04:36,690
However, for script objects, we actually want to change this from mono behavior to script object.

67
00:04:37,430 --> 00:04:43,310
The next thing I want to do to make this script will object usable is to head above this class declaration

68
00:04:43,310 --> 00:04:45,080
and add an attribute.

69
00:04:45,080 --> 00:04:51,260
So let's open up some brackets and we're going to use the Create Asset menu attribute.

70
00:04:51,470 --> 00:04:53,440
Now to explain what this does.

71
00:04:53,450 --> 00:04:55,130
Let's jump back over into unity.

72
00:04:55,130 --> 00:05:00,320
And unfortunately, we will be jumping back and forth between Unity and script quite a bit in this lecture.

73
00:05:00,710 --> 00:05:05,600
But if we wait for this script to compile and then anywhere in the project window, let's go ahead and

74
00:05:05,600 --> 00:05:06,620
right click.

75
00:05:06,830 --> 00:05:12,530
And if we go up to create, we can see we now have a question so at the top of this list.

76
00:05:12,530 --> 00:05:15,050
So that's what that Create Asset menu is doing for us.

77
00:05:15,050 --> 00:05:18,260
It's adding an option to create something in this list.

78
00:05:18,590 --> 00:05:24,170
And if we go ahead and click on this, we'll also notice it brings in a new script or object called

79
00:05:24,170 --> 00:05:25,250
New Question.

80
00:05:25,340 --> 00:05:26,750
So, but.

81
00:05:26,750 --> 00:05:29,540
Question so isn't the most descriptive of names.

82
00:05:29,540 --> 00:05:34,640
So to make this whole thing more usable, let's go ahead and just delete that test script table object

83
00:05:34,640 --> 00:05:41,240
I've just made head back over into our script and within this create asset menu within the parentheses,

84
00:05:41,270 --> 00:05:43,820
let's go ahead and add a menu name.

85
00:05:44,960 --> 00:05:51,950
And I'm going to change this to just be a quiz question and then to change the name of the file that

86
00:05:51,950 --> 00:05:53,150
gets created.

87
00:05:53,180 --> 00:05:58,250
Let's go ahead and use file name and call this a new question.

88
00:05:58,970 --> 00:06:02,510
If we go ahead and save that now and jump back into unity once again.

89
00:06:03,790 --> 00:06:08,950
Now if we right click and go up to our create menu, we now have a quiz question.

90
00:06:09,310 --> 00:06:15,700
If we go ahead and click on our quiz question, we get a script or object called New Question now because

91
00:06:15,700 --> 00:06:17,620
our script doesn't really contain anything.

92
00:06:17,620 --> 00:06:20,860
Our script to object currently does absolutely nothing.

93
00:06:20,860 --> 00:06:23,950
So let's fix that next and start adding some question.

94
00:06:23,950 --> 00:06:25,990
Text to our script to object.

95
00:06:26,920 --> 00:06:29,980
Back in all script within the class itself.

96
00:06:30,220 --> 00:06:37,390
Let's go ahead and add a new variable and this variable is going to be a string and we'll call this

97
00:06:37,390 --> 00:06:44,110
the question then to initialize this question string with some default text, I'm just going to say

98
00:06:44,110 --> 00:06:46,480
enter new question text here.

99
00:06:47,650 --> 00:06:49,480
Cut that off with our semicolon.

100
00:06:50,290 --> 00:06:54,130
Next, we need to think about the access level for this variable.

101
00:06:54,250 --> 00:06:58,150
Now we are going to need to be able to set this in the inspector.

102
00:06:58,150 --> 00:07:04,660
So your first thought might be to make it public, but we don't want this to be changed outside of our

103
00:07:04,660 --> 00:07:10,030
script to object, because if another class comes along and tries to edit our question, it's not going

104
00:07:10,030 --> 00:07:12,250
to be working on a copy of our question.

105
00:07:12,250 --> 00:07:15,040
It's actually going to be working on the question itself.

106
00:07:15,040 --> 00:07:20,380
So the text contained within our script to object will be changed on disk.

107
00:07:20,830 --> 00:07:24,760
So instead, let's make this a serialized field.

108
00:07:25,240 --> 00:07:31,330
This will give us the ability to set our question in the inspector, but not access this variable from

109
00:07:31,330 --> 00:07:32,530
another class.

110
00:07:32,530 --> 00:07:38,200
And we will actually need to be able to at least get the value of this variable from another class,

111
00:07:38,200 --> 00:07:41,560
but we'll worry about that in probably the next lecture.

112
00:07:41,590 --> 00:07:47,110
So with our serialized field in place, let's jump back to Unity to see the fruits of our labor.

113
00:07:47,320 --> 00:07:53,320
We can now see that we've got a new question string on our script to object with our default text already

114
00:07:53,320 --> 00:07:53,950
added.

115
00:07:54,310 --> 00:07:59,860
Now this is all perfectly fine and usable, but we'll find that as our text gets longer.

116
00:07:59,860 --> 00:08:04,870
So if I just add some additional characters here, we'll notice that it just sort of overflows off the

117
00:08:04,870 --> 00:08:05,320
edge.

118
00:08:05,320 --> 00:08:09,910
And so if your question is a little bit longer than just a handful of characters, it's going to be

119
00:08:09,910 --> 00:08:14,290
far more difficult to edit this later on and check it for typos and things like that.

120
00:08:14,470 --> 00:08:19,450
So to go ahead and fix this, let's once again jump back over into our script.

121
00:08:19,450 --> 00:08:23,140
And what we're going to do is add another attribute to our variable.

122
00:08:23,140 --> 00:08:29,320
So just above this, we're going to open some more brackets and I'm going to use the attribute text

123
00:08:29,320 --> 00:08:30,040
area.

124
00:08:30,040 --> 00:08:36,549
Now, what the text area attribute allows us to do is adjust and control the size of that text box in

125
00:08:36,549 --> 00:08:37,480
the inspector.

126
00:08:37,780 --> 00:08:39,460
This requires two parameters.

127
00:08:39,460 --> 00:08:43,870
It requires a minimum and a maximum number of lines that we want to display.

128
00:08:44,049 --> 00:08:51,220
So for our question, I think two would be a nice minimum size and maybe let's go up to six as the maximum

129
00:08:51,220 --> 00:08:56,110
size, because if our question gets any longer than that, it certainly isn't going to fit on the canvas

130
00:08:56,110 --> 00:08:56,860
itself.

131
00:08:56,860 --> 00:09:01,060
So for the last time, let's save our script and jump back into unity.

132
00:09:01,970 --> 00:09:05,030
And now we can see that our question box has now got bigger.

133
00:09:05,420 --> 00:09:11,410
And if we add some more lines, we can see we get a maximum of six lines visible to us at any one time.

134
00:09:11,420 --> 00:09:15,170
And if we shorten this down, we get a minimum of two.

135
00:09:15,740 --> 00:09:18,980
And I think that nicely brings us on to your challenge.

136
00:09:20,150 --> 00:09:26,660
If you haven't done so already, then decide on the topic for your quiz and then think of around five

137
00:09:26,660 --> 00:09:29,180
questions that you want to ask your players.

138
00:09:29,630 --> 00:09:35,000
Once you've got some questions down on paper, then go ahead and create the script objects that will

139
00:09:35,000 --> 00:09:39,830
hold these questions and our script object will only hold the questions at the moment.

140
00:09:39,830 --> 00:09:44,810
So for now, don't worry too much about the answers just yet because we'll get on to those in the next

141
00:09:44,810 --> 00:09:45,380
lecture.

142
00:09:45,410 --> 00:09:48,170
So pause the video now and give that a go.

143
00:09:53,380 --> 00:09:53,800
Okay.

144
00:09:53,800 --> 00:09:54,560
Welcome back.

145
00:09:54,580 --> 00:10:00,040
So hopefully you've now made a final decision on the topic for your quiz and you've created some scriptural

146
00:10:00,070 --> 00:10:02,350
objects to hold your questions here.

147
00:10:02,350 --> 00:10:07,180
I've already created four while you've been away in the questions folder, in my assets folder.

148
00:10:07,330 --> 00:10:13,300
And for the final one, all we need to do is right click, go up to create and then quiz question,

149
00:10:13,600 --> 00:10:14,620
give it a new name.

150
00:10:14,620 --> 00:10:19,660
So let's call this question five and then finally let's add some question text.

151
00:10:19,660 --> 00:10:23,410
So my game is all about helping people learn C-sharp and Unity.

152
00:10:23,410 --> 00:10:27,010
So I'm going to ask the question, what is a variable?

153
00:10:28,510 --> 00:10:32,980
However, since we don't have space to add our answers yet, we're going to leave things there.

154
00:10:32,980 --> 00:10:36,100
And in the next lecture we're going to look at adding this functionality.

155
00:10:36,100 --> 00:10:37,540
So I'll see you there.

