In this lecture, we're going to start creating the questions for our quiz using script table objects.
Before we go ahead and start doing that, though, there's a little bit of tidy up that we need to do
in our project because our hierarchy is starting to get a little bit messy and so is our asset folder.
So in our asset folder, let's clean this up first.
And the first thing I want to do is just store away our font assets in the text message folder.
So let's open that up and we're going to drag the font file into the fonts folder and we're going to
drag the font asset into resources, fonts and materials.
We've also got this button prefab, but this button doesn't really tell us what it does.
So let's give that a more descriptive name by just pressing F two and we'll call this the answer button
instead.
I will also create a new folder for that.
So right click create folder and we'll call this prefabs and drag that over into there.
That tidies up our asset folder.
So over in our hierarchy, let's do some work in here.
And I'm not going to worry too much about the pre visualization canvas or the background canvas, but
the children of our quiz canvas could certainly do with some better naming.
So I'm going to rename my text object to the question text.
And under our answer button group, I'm just going to rename our buttons to answer button.
And I'm also going to give it an index that starts at zero.
And since these are all prefabs, I can just go ahead and delete these and then duplicate again.
So we have answer button zero through to answer button three.
With all that set up.
Finally, let's just add a couple of new blank folders to our assets ready for the stuff that we're
about to do.
So the first thing I'm going to do is create a new folder called Script, which will hold all of our
C sharp scripts.
And I'm also going to create a folder.
Called questions which will hold all of our script objects.
Okay, so project all set up.
Let's actually talk about what script will objects are and how they're going to help us with our game.
So what is a script or object?
Well, it's really just a data container, so a way of storing data for our gang.
And they're useful because they keep this data out of our script.
And this helps us to save memory by storing all of that data in a single place that our scripts can
reference.
They also don't need to be attached to game objects like normal mono behavior scripts, and this makes
them very lightweight and convenient to use.
And probably the best way to think of scripts for objects is as a kind of template, so we can use them
as a kind of blueprint.
And this helps with data consistency.
Some examples of where script objects can be useful is for things like storing weapon statistics in
an RPG, so things like the damage and critical charts and things like that.
We could also use them to store card data in a collectible card game, and for our game we're going
to be using them to store question data.
So things like the question, text itself, all of the possible answers to that question and also a
reference to the correct answer.
Now I mentioned that the script objects kind of separate to the mono behavior scripts that you're familiar
with.
So on the one side, we have our code, which might be a mono behavior called quizzes.
And in the script, we might have various methods for displaying the question on screen and checking
the answer.
And we could store all of the question data in this script as well.
However, if we rip this data out of our script and store them in script objects, we could then reference
one of those script will objects when we need to get the question data that we're after.
And this will then just look at the data in the script object without actually copying it anywhere.
Now jumping back over into unity.
Let's actually create some script objects in our scripts folder.
Let's right click and go up to create new C-sharp script and we're going to call this the question.
So for script object, let's go ahead and open this up.
And the first thing I want to do in here is to actually delete the start and update method, because
not only are we not going to need it, we're not actually going to be able to use them with scripts
or objects.
Because if we look at the class declaration at the top here, we can see that it inherits from mono
behavior.
And this will be the case for every script that you've written so far in this course.
However, for script objects, we actually want to change this from mono behavior to script object.
The next thing I want to do to make this script will object usable is to head above this class declaration
and add an attribute.
So let's open up some brackets and we're going to use the Create Asset menu attribute.
Now to explain what this does.
Let's jump back over into unity.
And unfortunately, we will be jumping back and forth between Unity and script quite a bit in this lecture.
But if we wait for this script to compile and then anywhere in the project window, let's go ahead and
right click.
And if we go up to create, we can see we now have a question so at the top of this list.
So that's what that Create Asset menu is doing for us.
It's adding an option to create something in this list.
And if we go ahead and click on this, we'll also notice it brings in a new script or object called
New Question.
So, but.
Question so isn't the most descriptive of names.
So to make this whole thing more usable, let's go ahead and just delete that test script table object
I've just made head back over into our script and within this create asset menu within the parentheses,
let's go ahead and add a menu name.
And I'm going to change this to just be a quiz question and then to change the name of the file that
gets created.
Let's go ahead and use file name and call this a new question.
If we go ahead and save that now and jump back into unity once again.
Now if we right click and go up to our create menu, we now have a quiz question.
If we go ahead and click on our quiz question, we get a script or object called New Question now because
our script doesn't really contain anything.
Our script to object currently does absolutely nothing.
So let's fix that next and start adding some question.
Text to our script to object.
Back in all script within the class itself.
Let's go ahead and add a new variable and this variable is going to be a string and we'll call this
the question then to initialize this question string with some default text, I'm just going to say
enter new question text here.
Cut that off with our semicolon.
Next, we need to think about the access level for this variable.
Now we are going to need to be able to set this in the inspector.
So your first thought might be to make it public, but we don't want this to be changed outside of our
script to object, because if another class comes along and tries to edit our question, it's not going
to be working on a copy of our question.
It's actually going to be working on the question itself.
So the text contained within our script to object will be changed on disk.
So instead, let's make this a serialized field.
This will give us the ability to set our question in the inspector, but not access this variable from
another class.
And we will actually need to be able to at least get the value of this variable from another class,
but we'll worry about that in probably the next lecture.
So with our serialized field in place, let's jump back to Unity to see the fruits of our labor.
We can now see that we've got a new question string on our script to object with our default text already
added.
Now this is all perfectly fine and usable, but we'll find that as our text gets longer.
So if I just add some additional characters here, we'll notice that it just sort of overflows off the
edge.
And so if your question is a little bit longer than just a handful of characters, it's going to be
far more difficult to edit this later on and check it for typos and things like that.
So to go ahead and fix this, let's once again jump back over into our script.
And what we're going to do is add another attribute to our variable.
So just above this, we're going to open some more brackets and I'm going to use the attribute text
area.
Now, what the text area attribute allows us to do is adjust and control the size of that text box in
the inspector.
This requires two parameters.
It requires a minimum and a maximum number of lines that we want to display.
So for our question, I think two would be a nice minimum size and maybe let's go up to six as the maximum
size, because if our question gets any longer than that, it certainly isn't going to fit on the canvas
itself.
So for the last time, let's save our script and jump back into unity.
And now we can see that our question box has now got bigger.
And if we add some more lines, we can see we get a maximum of six lines visible to us at any one time.
And if we shorten this down, we get a minimum of two.
And I think that nicely brings us on to your challenge.
If you haven't done so already, then decide on the topic for your quiz and then think of around five
questions that you want to ask your players.
Once you've got some questions down on paper, then go ahead and create the script objects that will
hold these questions and our script object will only hold the questions at the moment.
So for now, don't worry too much about the answers just yet because we'll get on to those in the next
lecture.
So pause the video now and give that a go.
Okay.
Welcome back.
So hopefully you've now made a final decision on the topic for your quiz and you've created some scriptural
objects to hold your questions here.
I've already created four while you've been away in the questions folder, in my assets folder.
And for the final one, all we need to do is right click, go up to create and then quiz question,
give it a new name.
So let's call this question five and then finally let's add some question text.
So my game is all about helping people learn C-sharp and Unity.
So I'm going to ask the question, what is a variable?
However, since we don't have space to add our answers yet, we're going to leave things there.
And in the next lecture we're going to look at adding this functionality.
So I'll see you there.