In this lecture, we're going to finish off our script to objects so that they also hold the answers
to our questions.
And in the process, we're going to learn a little bit more about arrays.
So here we are in one of our scripts or objects which is currently only holding the question text.
So let's have a think about how we're going to store the answers to our quiz.
Well, on our canvas we have four buttons, so one for each answer.
So your first instinct might be to store each answer as its own unique string.
This is a perfectly fine approach, but it is a little bit cumbersome and not very flexible.
So rather than doing this way, what we're instead going to do is use something called an array.
So what is an array?
Well, you can think of them as a grouping of multiple variables of the same type.
So if we think about this in terms of our answers, rather than having four unique string variables,
we can have a single array of strings.
Now, when we group variables into an array, each item that we store in that array is called an element,
and each element can be accessed within the array by its index number.
So they're very easy to work with, especially when we start looking at looping through them.
Now one thing to note about arrays and a lot of things in computers, in fact, is that counting starts
at zero.
So the first element in an array is element zero.
And this is initially quite confusing if you're not used to this zero indexing that computers use,
but with a little bit of time and a little bit of practice, this will soon become second nature to
you.
So let's look at an example of what an array might look like.
Well, here we have an array of odd numbered integers, and specifically we've got the first five odd
numbers.
Now, just like any other variable, we have declared the type at the front.
So this is storing integer values.
But notice these brackets after the type declaration, these brackets indicate that we're now dealing
with an array of integers rather than just a single integer.
We have our name of our variable just like any other.
So we've called this one the odd numbers and we've initialized it with the first five odd numbers.
So notice that we've used these braces on either side and then we have five elements inside this.
But because we start counting at zero, each element within that would have an index of zero, one,
two, three and four.
And again, this can be a little bit confusing if you're not used to this zero indexing, but please
do trust me on this, that the more that you work with them, the easier this does become.
Now, this isn't the only way to initialize an array, so we don't necessarily always know what values
we want to place in the array.
And when that happens, what we can do is we can initialize it like this.
So the front is exactly the same.
We've still got an array of integers called odd numbers, but now we're initializing it as a new array
of integers with a size of five.
So we're saying that this array will have five slots, if you like, five elements contained within
it.
And then later on we might access one of those elements by its index number and then change that.
The same we might change a regular old integer.
So let's jump over into our script object now and see these arrays in action.
Now the answers to all questions are generally going to be strings and so make it into an array.
I just need to open some brackets.
I'm then going to give my variable a name.
So let's call this the answers.
And because I don't necessarily know what the answers are going to be ahead of time, all I know is
that each question will have four answers.
I'm going to initialize my array by saying that it's equal to a new string array.
So with the brackets open there and within these brackets, I'm going to put the number four.
So there's going to be four elements to my array.
The next step now we've set up the core of our variable is to think about the access level that it needs.
So just like our question, we're going to want to be able to change them in the inspector, but we
don't want to be able to change them from outside the script object.
So for this, I think a serialized field is again a very good choice for the access.
And if we save our script table object and jump back into unity, we'll see that we now have this rolled
up option for answers in our script or objects so we can unroll this and we have four elements and notice
the element start counting from zero up to three and we can go ahead and then fill in the potential
answers to our question.
Now this question has some answers attached to it.
We now need to know which one is the correct one.
So now we're going to need another variable attached to our script to object to tell us which element
of this array holds the correct answer.
So back over into our script will object.
Let's again create another new variable and this time it's going to hold the index number for our answer.
So before you just follow along with me, see if you can write this variable for yourself and this isn't
a full challenge.
So I'll just give you a moment to think before carrying on.
Okay.
So this is going to be a integer type because we're asking for an index number for our array and I'm
going to name this the correct answer index.
Now we need a way of setting this in the inspector and we still don't want this to be public to other
scripts in our game.
So let's again use a serialized field and that will now give us a way of storing the index to the correct
answer.
So if we start, say, the number three as our correct answer index, this would relate to the fourth
element of our answers array because again, remember that this starts counting at zero.
Next, we want to go ahead and finish off our script object.
So just like with our public getter method for our question, we're going to need a getter method for
the correct answer index and another getter method to get an answer at a specific index.
And I think that brings us nicely on to your challenge.
I want you to create two more getter methods.
The first one is going to be called Get Correct Answer Index.
So think about the access level of the method, the return type of the method and what variable it's
actually going to return.
And the second one, which is a little bit more complicated but not by much, is going to be called
get answer.
And I say this one is a little bit more complex because this one is also going to accept a parameter
of type int, which is going to be called index, so that when you use this method from outside the
script, you can pass in the index for the answer that you want and get that particular answer out of
our answers array.
So pause the video now and give that one a go.
Okay.
Welcome back.
So hopefully you got on well with both of those, but don't worry too much if you struggled with the
second one.
Let's go through both of them now for the first one to return the correct answer index.
Our getter methods are always going to be public and in this case it's going to return an integer.
And we said that we were going to call this one get correct answer index.
I know this one is going to do is return the correct answer index.
So congratulations if you manage to do that first one without any problems.
And now let's look at the second one.
And I'm actually going to put this one just above.
It doesn't really matter where you place it.
So if you put it below, then that's absolutely fine as well.
And again, it's a getter method, so it's going to have to be public so other scripts can use it and
it's going to return a string.
And this is just a single string, so not an array of strings.
And we said this was going to be called get answer.
We also said that this was going to accept a parameter of type int called index and for this one we
want to return the answers and the index that we specified.
So what this method is doing is it's passing in an index that we want to use and then just returning
the answer at that given index.
And if we go ahead and save our script and head back over into unity, we now have our complete script
for object.
We have space for the question.
We have space for all four answers, and we can also specify the index for the correct answer.
So in this case, the correct index is zero, but we can place any number in there that we like.
So it might be that we need the correct answer index to be two.
Now, one thing to be very careful of going forward is that this correct answer index is currently unconstrained.
So we can have the correct answer of -56 or the correct answer of 54, and we should probably try and
prevent that.
So the designer doesn't accidentally put in something crazy, but for now, we'll leave it as it is,
and we may come to fix that in a later lecture.
For now, though, I think we've spent long enough on our script to object, and it's about time we
started actually displaying our questions on the canvas that we worked so hard on.
So that would be the subject of the next lecture where we finally start displaying all of this information
on the canvas.
So I'll see you there.