In this lecture, we're going to carry on connecting up our UI, and this time we're going to connect
the answer text to the answer buttons.
So let's jump straight into our quiz script.
And the first thing I want to do in here is to create a new variable to store our buttons.
Now for our variable type, we could go ahead and store the button component directly, but because
we're not really going to be doing much with that button component and instead doing things like changing
the text components and the image components, what we'll actually do is create this variable to be
of type game object.
And since we have four buttons for our four answers, let's make this an array.
So remember to add those brackets at the end of the type declaration.
Next, our variable is going to need a name, so I'm going to call these the answer buttons.
And for the access level, I'm again just going to go with a serialized filled.
Now if we go ahead and save our script and head back over into unity.
If we select our quiz canvas and scroll down in the inspector, we now have an array for our answer
buttons and this list is currently empty.
Now we could go ahead and just drag in each one of our game objects into here to populate them one at
a time.
But a quicker option is to actually go up to this padlock in the top right corner.
And then if we select all of the objects that we're interested in, the inspector won't change and we
can go ahead and drag all three of the remaining ones or all four.
If you didn't do the first one and drop them into this array now that's all populated with all of our
answer buttons, we can go ahead and unlock the inspector.
So click on the padlock item once again.
Now let's go ahead and think about how we're going to change the text on these buttons.
If we take a look at the composition of one of our buttons, the text component that we actually want
to change is a child of a button game object.
So in our script, rather than trying to get a component from the root object, we're going to look
at getting a component in the children.
So let's jump back over to our script down in the start method.
Let's go ahead and look at how to get that text component from the child object.
Well, for this, I'm going to set up a temporary variable of type text mesh pro you gooey and I'm going
to call this button text.
And for the moment, we're just going to worry about the first answer for our question.
So I'm going to take the answer buttons and I'm going to look at the very first element.
So element zero and because this is a game object, I can go dot get component in children.
And the type of component I want to find in the children is going to be this text mesh pro.
You going?
And this we'll now look through all of the children of our answer button and find the first text mesh
pro component that it can.
And now we have that reference.
We're going to change the text on that button to the first answer from our stored question.
And this is going to be your challenge.
I want you to change the text being displayed on our button to the first answer that's stored in our
script or object.
So this is going to be a similar process to how we got the question.
Text from our script will object, but remember that getting the answer also requires us to pass in
the index of the answer that we want.
So pause the video and give that a go.
Okay.
Welcome back.
So hopefully you got on well with that.
And to cover off how we do this, we're going to say that the button text, text.
It's going to be equal to our question to get answer.
And because we're currently only interested in the very first answer, we can just pass in the index
of zero.
Now if we go ahead and save our script and head back over into unity.
And now if we go ahead and hit play, we should find that the question is being pulled in, as is the
first answer.
So we're a quarter of the way there, and now we just have to worry about the other three buttons.
So let's come out of play and jump back to our script.
And just like we've done this first one, we could just copy and paste these two lines another three
times and then just change the index.
So we have index zero up to index three.
However, this is going to make our code look absolutely terrible.
It's going to be difficult to manage and a nightmare to debug.
So instead what we're going to do is use a loop and specifically we're going to use a for loop.
So what is a loop?
Well, a loop in code really just repeats an event until some condition gets met.
And they're very powerful for counting or iterating.
And one type of loop and the loop that we're interested in is called a for loop.
And a for loop allows us to loop through our code a set number of times.
So let's look at what one of these for loops actually looks like.
So here we have the basic format for a for loop.
And if we take a closer look inside the parentheses of the for loop, we'll notice that it's kind of
broken into three sections, each delimited by a semicolon.
So this first piece of the loop is known as the initial teaser, and this part of the for loop runs
at least once before the code block underneath is executed.
And we use this initial teaser to set up something called the iterator.
And you can think of the iterator really as just a type of counter.
So we're creating a temporary variable called I, and we're setting this to zero.
So I, in this case, is our iterator.
Next up, we have our second statement.
And this is the condition.
So while this statement is true, our for loop will run.
So in this case, our for loop will run while our iterator ie is less than some number n.
Now, when you're writing these conditions, it's important to be mindful and be careful of infinite
loops because an infinite loop will crash your program and generally lead to you having a bad time.
Finally we have the iterator phase of our for loop and this part executes at the end of each loop.
And as I'm sure you may be able to guess from the syntax, we use this to either increment or decrement
or iterator.
So in this case, we've got A-plus plus plus.
So on every loop I is going to be getting increased by one.
So with the basic syntax and structure out of the way, let's actually create one of these for loops
to loop through our buttons.
Back over in our quiz script, let's create some space because these two lines are actually going to
go inside of our for loop.
So let's create the for loop by saying full and then opening some parentheses when they need to initialize
our iterator.
So we're going to say int i equals zero and we can really set this to any variable name and any number
that we like.
But since we're going to be looping through an array which starts counting at zero, then this seems
like a sensible option.
We can then place a semicolon to end that part of the statement.
And next we need to think about when we want this loop to run.
Well, we want this loop to run while IE is less than the length of our answer button array and we can
get the length by saying Answer buttons length.
Finally we need to set up the iterator condition so every loop we want to increase ie by one.
So let's put C++.
Then we'll create the code block and just copy and paste these two lines of code into their.
Let's do some quick tidy up and we now have our loop.
Finally, there's one more thing we need to change, and that is that we're currently only looking at
the first answer.
So if this was to run at the moment, we would loop through this four times.
But every time we do that, we're just going to be setting the same answer button with the same answer.
So let's change this zero to our eye.
And again in I'll get answers.
And now every time I gets increased by one on each loop, we're going to be getting the next button
in our list and getting the next answer from our question.
So let's go ahead and save this script and see this in action, in unity.
And if we go ahead and hit play.
We now have our question being asked and all four buttons receiving an answer.
So hopefully you can see the power that four loops give us in both cleaning up our code and automating
the repetitive tasks that we would otherwise have to write in our code.
Now, our quiz is certainly coming along, but we still can't click on any of our buttons to check our
answers.
And we also only have one question in our quiz.
So in the next lecture, we're going to start remedying that and checking the answers that the player
has clicked on.
So I'll see you there.