Okay, everyone, it's been a long time coming, but we're finally going to be adding our other questions
to our quiz.
And to do this, we're going to be using something called a list.
So what is a list?
Well, they're kind of like arrays, which we've already seen when we were storing our buttons.
So if we consider the similarities to an array, a list is still a grouping of multiple variables of
the same type.
Each item stored within our list is still called an element.
Each element can be accessed by its index number.
And counting again starts at zero.
So there are the similarities, and you may currently be thinking that they're virtually identical.
But the big difference between a list and an array is that lists are mutable, meaning that we can change
their size.
So with an array, once it's created, we can't change the size by the list.
We can add and remove items as much as we like.
So let's look at the syntax of how to create a list.
If we first consider the syntax for an array, then we can create a new array like this.
But for a list, the syntax is slightly different.
First, we're going to specify that we want a list, and then inside the angled brackets or chevrons,
we're going to tell the list what type it's going to be storing.
So in this example, we're storing a list of integers.
We then have the name of our variable, which in this case is odd numbers, and then we can initialize
this list by saying new list of type integer.
So that's how we're going to be creating a new list.
But now let's look at some useful methods that are going to come in handy.
First of all, if we want to check the item count within our list, we can use list count.
So this is very similar to the array length that we were using before.
Next, if we want to check that an item exists within our list, we can use list DOT contains and then
we can pass in the thing we're looking for.
So with the example of storing our odd numbers, this would ask whether the number three currently exists
in our list or not.
If we'd like to add a new item, we can use list, add and then tell it what we want to add.
So in this case, the number three, we can remove an item with list, remove and again pass in what
we want to remove if it exists, if we don't know a specific value that's contained in our list and
we just want to remove the item at a given index we could use list remove at and then passing the index
that we're interested in.
So in this case, remove at zero would remove the first element in our list.
And finally, another good one to remember is list dot clear, which will completely empty our list.
So with explanations out of the way, let's go ahead and create our list for our questions.
However, before we go ahead and do that, there's one thing I want to change about our current code,
and that's our current serialised field for our current question.
Once we've created our list, we're no longer going to need this serialized in the inspector.
So let's go ahead and clear things up by just removing that serialized filled attribute.
And the other thing I want to do is actually rename this variable because our list is going to have
a very similar name.
And to avoid errors and confusion as we continue to code, let's go ahead and rename this by pressing
F two and instead of question, let's call this the current question.
With that renamed that should have replaced any instance of our old question with current question.
So if we have a quick scroll down, we can see that in display answer.
It's now looking at the current question instead.
And in display.
QUESTION We're also looking at the current question so minor code fixing aside, let's go back up to
our variables and let's go ahead and create our list.
We're going to want to drag over our questions in the inspector.
So let's make this a serialized field and we can create a new list by saying list of type question.
So, so our question, script table objects and we'll give this variable the name questions, which
is why we wanted to change our old question variable to current question.
Now, because this is serialized, we can go ahead and leave this as it is.
But if you want some added protection in case you remove the serialized field later on in your journey,
we can instantiate this list by writing equals new list of type question.
So, so with our list set up and once we've dragged the questions over in the Inspector, we're going
to need to get questions from this list and make them the current question.
So let's scroll down to our get next question method.
And now when we get a new question, we're instead going to want to get the question from our list.
Now, there are many ways we may decide to get these questions from our list, but to add a bit of randomness
to our game, I'm going to be pulling a random question out of our list.
Whenever we need to get a new one, we're going to do this in a new method.
So once we've set the button state and the button sprites, but before we display our question, let's
go ahead and write a new method called Get Random Question.
Then we're going to need to write this method.
So underneath our next question, let's create a void, get random question method.
Let's start by getting that random number.
So we're going to have a temporary variable of type int called index.
And this is going to be equal to random range with a minimum of zero and a maximum of our questions
count.
So this is going to return a random number between element zero of our list and the last element in
our list.
With that index, we can then say that our current question is going to be equal to the question in
our list at that index, and then we want to go ahead and remove that question from the list so we can
say questions, remove the current question.
Now, this should all be fine, but whenever I remove an item from a list, it's always good to just
double check that it actually exists to avoid any potential errors cropping up and just to err on the
side of caution.
This shouldn't strictly be necessary for our game because it is very small and we're not going to be
doing much else with our questions list.
But in larger projects, when we start moving things around and we have a lot of things talking to each
other, it's always good to make sure this check happens so that we don't accidentally run into a problem
that may cause our game to throw an error.
And we can check to see that our current question exists in our list by saying If questions DOT contains
the current question, then we want to go ahead and remove it.
Now, with that set, let's go ahead and save our script and jump over into Unity to test it all out.
If we look in the inspector under our questions header, we now have a list of questions that we can
use.
So let's lock the inspector so that we can drag our questions over and over to the questions folder
where we stored all of our script objects and then select all of them and drag them over into our list.
Now for me, that's put them in reverse order, but because I'm pulling them at random, it doesn't
really matter too much.
If you want to change the order of any of the elements, you can just click and drag to move them up
or down.
And if you want to add or remove any other element, then we can either use the plus and minus buttons
at the bottom or we can change the size of our list with this input field.
So questions all set up, we should now be picking questions at random.
So let's go ahead and hit play to test this out.
And before we go ahead and answer a question, I'm going to pause the game and click back on my quiz
canvas so I can see the questions that are in our list, and we'll notice that two of them have actually
been removed rather than just one.
Now, the good news is the questions were randomly selected, but why did we remove two of them?
Well, there's a very logical explanation for this, which we'll fix in just a moment.
But for now, let's just do a little bit more testing to make sure that it doesn't happen for every
single question.
So let's unpause our game and wait for the timer to run down.
And for all subsequent questions, it does look like it's just removing the one.
So if we quickly skip through the rest of our quiz, well, notice that once we've answered the final
question and our list is empty, then if we check the console, we're getting an error saying that the
index was out of range, i.e. we're trying to pull an item out of a list when the list is empty.
So two bugs to fix there.
So let's jump back over into our script.
And the first bug of removing two questions at once is incredibly simple to fix because if we head up
to our start method, we're currently getting the next question in START and we don't actually want
to be doing that anymore.
So let's delete that line and also our commented outline as well.
And that should fix the problem with two questions being selected on the first run through.
And then to fix the error that comes up when we've run out of questions in our list, let's head down
to our get next question method and we're going to put in a check here.
And I think this would make a very good challenge for you.
I want you to go ahead and modify our get next question method to check whether there are still questions
left in our list before we go ahead and execute those other four methods.
So pause the video now and give that one a go.
Okay.
Welcome back.
So hopefully you got on well with that challenge.
And the solution to this problem is going to be to use an if statement and ask if the questions dot
count is greater than zero.
Then we want to go ahead and run these four methods.
So let's go ahead and test this out to see if everything's worked through the magic of video editing.
We can find that both of these bugs have now been fixed.
So congratulations.
We now have a game with multiple questions and at this stage you can feel free to add as many other
questions to your game as you like.
Now, the next thing we're going to want to look at in our game is to improve the UI a little bit because
we now have multiple questions, but the player has no idea how far through the game they are.
But that's a topic for another lecture, so I'll see you there.