All game is now at the stage where the player can click on one of the answer buttons and be told whether
they got the answer right or wrong.
But we do have a little bit of a bug here because even though we've now selected an answer, there's
nothing to stop us from selecting a new answer and potentially changing our result.
And the thing that's controlling this is on the answer buttons.
So if we go ahead and click on one of our answer buttons and scroll down to look at the button component,
we have this interactive toggle flag when it's checked, that means the button can still be clicked
on and if we disable it, then the button will no longer work.
So we want to look at doing this programmatically and disabling the buttons when an answer has been
selected.
So let's jump over into our quiz script.
And before we go ahead and do this, I do want to do a little bit of refactoring and tidy up our start
method.
This was some of the first code we wrote, and whilst it's fine to stay in the start method for now,
it really is a good idea to tidy that up and put it in its own method so that we can reuse it later.
And also just to wrap it up with a nice name so that we can remember exactly what it does.
So let's scroll down and make some space for it and keep all of our code tidy.
If we hover over by the line numbers, we can actually collapse down sections of our code to hide it
away.
So underneath our public method, we're going to create a new private method, and this doesn't have
to return anything so we can make it a void.
And we're going to call this method display question.
Because if you remember this code in start at the moment, pretty much just displays the questions and
answers on the canvas.
So let's then go ahead and cut and paste all of our code into that new method, and then we're going
to go and call this method from within start.
So not a drastic change to our code or performance there, but whenever we do things like this, it
is a good idea to just save the script and pop back into unity.
To double check that we haven't broken anything.
So if you want to err on the side of caution, then go ahead and do that now.
I'm not going to worry too much.
I'm fairly confident that I've got it right.
And now we can start thinking about our buttons.
So let's have a quick look at the game flow that we're after.
First of all, we're going to display a new question to the player.
And in doing so, we're going to turn on all of the answer buttons.
Then once the question has been answered, we want to turn all the buttons off and then loop back around
to potentially display a new question.
So for turning the buttons on and off, we could have two separate methods for this one to set them
all to true and one to set them all to false.
But because the logic for both is incredibly similar, we're going to look at killing two birds with
one stone and just writing a single method.
So back into our quiz script and at the bottom underneath display questions, I'm going to create another
new method that will set the button state to whatever we pass in.
This method doesn't need to return anything so we can make that void and we'll call it set button state.
Then to tell it what state we want.
We're going to pass in a bill and call this the state.
So that's just going to be true or false.
This method is then going to have to loop through all of the answer buttons.
So very similar to what we're doing when we're displaying a question up here.
So let's say that for int i equals zero.
Our condition will then be when I is less than the answer button's length and our iterator will be going
up by I.
Plus plus.
Next, we need to find a reference to the button component on our answer button game object so we can
have a type button and we'll call this the button and we're going to set this equal to the answer buttons.
At index, I get component of type button.
With that reference to the button in hand, we can then go ahead and say button dot intractable.
I are not is intractable.
That actually tells you the state that it's currently in we want dot intractable.
And that will allow you to set that flag.
So do be careful.
There are a few methods dotted around unity that are like that that you may think do one thing, but
actually do something else, but with the correct one in hand.
Now the button interact table, we're going to set this equal to the state that we're passing into our
method.
So that will either set that flag to either true or false.
Next, we need to call this from somewhere.
And we said we want to set this button state when we get a new question and put it on the canvas and
also when the answer has been selected, well, let's do the answer selected version first.
So let's scroll up to where our on answer selected method is.
It's currently rolled up, so we just need to go next to the number line here and unroll that down and
then down at the bottom of that method.
Once we've done all of our other work, let's go ahead and call that set button state method and we're
going to pass in false because we want to disable the buttons at this stage.
So that takes care of disabling the buttons, but we are going to need to re-enable them at some point.
So let's have a think about where we're going to do that now.
We could put it in display question because that's roughly when we're going to want to do it.
However, later on, we are going to be doing some other stuff around getting a new question as well.
So rather than making this method do more than one thing, let's create another method above it.
And this again is going to be a void.
And we're going to call this one get next question.
Then inside this method, we're going to set the button state back to true so the player can click on
the buttons again when we get a new question.
And we're also going to display the question.
So with that set up, let's save our script and jump back into unity.
Now, if we select an answer and then try and click on a different answer, we'll notice that all of
the buttons have been disabled.
So that's the disabling of the buttons now working.
And unfortunately, at the moment, we're not going to be able to see the re enabling of the buttons
because we do only have one question in our quiz.
So for now, we're just going to have to kind of hope that that works.
And if we have any problems, we can always come back to it later.
So as a developer, it may just be worth making a note to say that that feature is currently untested.
And talking of untested features.
There is one more thing that I want to do in this script, because it's going to set us up nicely for
a later lecture, and it's also a very good excuse to have a challenge because in addition to resetting
the button state, when a new question is presented, we're also going to want to reset the button sprites
so that we don't continue to highlight the old answers that we had from the previous question.
So when we get our next question, which again, we're not currently calling at the moment, we're going
to set our button state to true.
We're then going to set the default button sprites.
And that's a method that we're about to write and then we can go ahead and display our new question.
So let's go ahead and write this method.
And we'll do that down at the bottom underneath set button state.
We can say void set, default button sprites.
And writing this is going to be your challenge.
I want you to write this set default button sprouts method.
Now, using the code that we've already written in this section, you should be able to piece together
how to do this on your own.
But if you would like to hang around for some extra logic, then the logic for this method is going
to first be to loop through all of our answer buttons, which we've done many times now.
We then want to get the image component off of each one of those buttons and change the sprite back
to our default sprite.
So pause the video now and give that one a go.
Okay.
Welcome back.
So hopefully you got on really well with that challenge because it is something we've done a few times
now.
So for this, we're going to need a for loop to loop through our buttons so we can say four int equals
zero.
Our condition will be while I is less than the answer buttons length and we're going to iterate by one
every loop.
We then want to store a reference to an image component so we can say image button image equals the
answer buttons.
At index I get component of type image and then we can set the button image sprite to be equal to the
default answer sprite.
So congratulations if you manage to complete that challenge and piece all of that together and to just
double check that this is all working, let's actually go up to start.
And rather than calling display question here, let's call get next question because at the moment this
should be fine for testing.
Let's then jump back into unity and just go ahead and kind of break things just a little bit.
So on our buttons we're going to turn them off by default for testing.
And I'm also going to change the sprites to the answer sprite by default.
Now what this will mean is that if our methods that we've written have worked, then when we press play,
these buttons will change back to our blue default sprite and the buttons will be clickable.
So let's go ahead and hit play and test this out.
And indeed everything has worked.
And now that everything's working for a single question, I think it's time to start adding our other
questions to our game and looking at how to switch between them.
So that's all for now, and I'll see you in the next lecture.