In this lecture, we're going to be creating the game manager, which is going to control the flow between
our quiz screen and our wind screen.
And this will mark the end of our project.
So let's go ahead and create our game manager over in the hierarchy.
Lets right click and create a new empty object.
I'm going to reset the transform and then rename the object to be the game manager and let's go ahead
and create a new script in our scripts folder.
So right click Create C-sharp script and call it the game manager.
And you'll notice with this script it's been given a brand new icon, and that's because game managers
are incredibly common.
So Unity has kindly changed the icon for this type of script so that we can easily find it buried in
our assets folder.
Let's go ahead and drag our game manager script over to our game manager object and then we're going
to go and open that up.
Now before we worry about reloading levels and things like that, let's just set up the transitions
between our quiz canvas and our win canvas.
So let's go ahead and tidy up the comments above starts and update.
But we are going to need both of these methods, so we'll leave them intact for now.
Up at the top, we're going to need a few variables.
We're going to need access to our quiz script so we can say quiz and we'll call it the quiz.
And we need access to our end screen script.
So we will call this the end screen.
And in start, let's go ahead and find both these objects.
So our quiz is going to be equal to find object of type quiz.
And our end screen is going to be equal to find objects of type end screen.
Next, we're going to want to toggle the visibility of these canvases so that our quiz canvas is visible
when the game starts and our end screen is hidden.
And then later on in the game's lifecycle, these will swap over.
So also in start, let's say that our quiz game object dot set active equals true and our end screen
game object dot set active equals false.
So what this is doing is taking the game object itself and then changing its active state in the hierarchy.
So if we jump back to unity, it's just like toggling the little flag next to the name up in the inspector.
So if it's true, we can see it.
And if it's false, it'll disappear.
While we're here in our hierarchy, let's enable both our quiz canvas and our win canvas.
It makes things look a bit messy, but that's okay because if we go ahead and hit play, then our game
manager is going to sort everything out for us and it's going to enable our quiz canvas and disable
our win canvas.
Next, our game manager is going to want to toggle the states for our two canvases when the game ends.
So back over in our script, we're going to do this in update and this is going to be your challenge.
I would like you to check whether the game has been completed and if it has, then we want to disable
the quiz canvas and enable the win canvas.
So pause the video now and give that a go.
Okay.
Welcome back.
So to check whether the game is completed, we're going to need an if statement.
And if you recall in our quiz script, we have a public variable called is complete.
And this was being set when the progress bar was completely filled.
So if our game is complete, we want to take our quiz game object and change the set active to false.
And we want to take our end screen.
Dot game object.
Dot set active to true.
Finally, for this method, we're going to want a way of reloading our scene.
If the player clicks the play again button and we can do this using the unity scene management system.
So at the top of our script, let's add another using statement.
And this time we're going to use unity engine dot scene management.
We can then create a new method that we can attach to our button.
So let's create a public void and we'll call this on replay level.
And to reload the scene.
We want the scene manager to load scene and in here we have a multitude of options and if we scroll
through, we're going to use this option here where we pass in the scene, build index and to get the
build index of the current scene, we can say scene manager dot get active scene dot build index.
So this is getting the build index of the currently active scene and we're just telling the scene manager
to load that scene again.
That will refresh our quiz and all of our scores and allow the player to play the game again.
So let's go ahead and save our game manager script, jump back into unity and on our win canvas we want
to go to our replay button, scroll down and in our on click event we're going to add the plus icon.
Drag over our game manager.
And then in the dropdown we're going to select the game manager and then on replay level, now it's
finally time to test everything out and check that everything our game is working correctly.
So let's go ahead and hit play.
And we find that after we've answered all of our questions, our windscreen is popping up and our quiz
canvas is being hidden.
But our score message has not been updated.
And that is because we've forgotten to call the method that does that.
But what we're hearing is at least double check our play again button.
And the game does seem to reload.
But we've also got a warning message down here and it's saying we've got a problem with our display
answer on line 78.
So let's look into both of those problems.
Head over into our game manager script first, because when the game is completed, as well as setting
our end screen to be active, we also want to take our end screen dot show final score that should update
that text field for us to show us our final score and for the error that's showing up when we reload
the game, let's head over to our quiz script.
And the problem exists in our update method where we're currently checking to see if we should be loading
an exit question.
And if we are, then we need to get the next question.
Otherwise we're going to be displaying the answer and setting the button state.
And this error is popping up because on the very first update loop of the game, this statement is currently
false and this statement is true.
So we're trying to display an answer, but we haven't actually set a current question yet.
Now this bug can take a little while to track down, but I found the quickest and easiest way to fix
this problem is to go up to where we're creating our has answered early boolean check and just setting
this to true to start with this means that on the very first loop, if we head back down to our update
loop, this check is going to fail.
So we're not going to try and display an answer.
Then on the next update loop, this will be true and everything will work as intended while we're here.
You may have also noticed during testing that when we answer the final question, rather than giving
us time to check the answer, it just jumps straight to our windscreen.
And this is to do with where we're setting our is complete flag.
So we're currently doing it when we select the answer, which is automatically throwing us to that windscreen.
So if instead we cut this out of here for now.
And what I'm going to do is I'm going to pop it in my update loop when we check for a new question.
So now when we answer the final question, we're going to wait for that timer to run down so we can
review the answer.
And then in update, it's going to try and load a new question, which isn't going to exist in our list,
but we're going to catch that with our if statement we can say is complete to true.
And then to avoid getting another question and potentially causing an error, let's just return out
of our update loop early.
Finally, before we test everything out for one last time, there's also another bug that you may see
and it's again a null reference exception error.
And this time is to do with the end screen.
But since this bug can show up all over the place, let's tackle it on all of the scripts we've written.
And the problem comes from finding objects in START.
Now to explain this further, let's have a look at the Unity script execution order.
We already know that START will be called when the game starts and before the update loop.
But as you can see from this flowchart, there's also a few more options that are available even before
start.
And the method that we're interested in here is the awake method.
Just like Start Awake is only ever called once for the lifetime of the game object.
And it's good practice whenever we're finding objects in our scene or getting components set up for
our scripts that we do these things in a wake because all of the scripts will run their awake method
and then call their start methods.
So if we jump back over into our script, what we're going to do is whenever we're finding objects in
START, which is going to change our start method to awake.
So in our end screen script, we're going to change start to awake so that our scorekeeper can be grabbed
nice and early in our quiz script.
We're doing some other stuff like setting up progress bars, but because these are related to the quiz
script, it doesn't matter if these also happen in a week.
So let's go and change our start method to awake in here as well.
In Scorekeeper.
We don't have a start method at all so we can leave that as is in our game manager.
We're finding some objects of type and setting things to active, and this is a situation where a start
and an awake method will come in useful.
So above start, let's create an awake method.
In a wake, we're going to cut and paste our finding objects of type.
And then install it.
We're going to set whether these objects are active or not.
So with that aside on script execution order out of the way, let's go ahead and save all of our scripts
and jump back into unity.
Let's go ahead and hit play.
We can once again see that our win canvas is being hidden.
And if we go ahead and jump through our quiz very quickly, we'll find that our quiz canvas becomes
hidden after answering the last question and our win canvas becomes active.
Our score is correctly being updated and displayed, and if we go ahead and play again, then the game
will reload and we'll get no errors at any stage during our game.
So that now brings us to the end of the section and we now have a working game.
The only thing left to do is to add as many questions as you want and give yourselves a big pat on the
back for finishing your quiz master game.