1
00:00:03,770 --> 00:00:08,240
In this lecture, we're going to be creating the game manager, which is going to control the flow between

2
00:00:08,240 --> 00:00:10,340
our quiz screen and our wind screen.

3
00:00:10,340 --> 00:00:13,040
And this will mark the end of our project.

4
00:00:13,840 --> 00:00:17,860
So let's go ahead and create our game manager over in the hierarchy.

5
00:00:17,860 --> 00:00:20,920
Lets right click and create a new empty object.

6
00:00:21,040 --> 00:00:27,520
I'm going to reset the transform and then rename the object to be the game manager and let's go ahead

7
00:00:27,520 --> 00:00:30,220
and create a new script in our scripts folder.

8
00:00:30,220 --> 00:00:35,200
So right click Create C-sharp script and call it the game manager.

9
00:00:35,830 --> 00:00:41,860
And you'll notice with this script it's been given a brand new icon, and that's because game managers

10
00:00:41,860 --> 00:00:43,450
are incredibly common.

11
00:00:43,450 --> 00:00:49,480
So Unity has kindly changed the icon for this type of script so that we can easily find it buried in

12
00:00:49,480 --> 00:00:50,590
our assets folder.

13
00:00:50,980 --> 00:00:56,020
Let's go ahead and drag our game manager script over to our game manager object and then we're going

14
00:00:56,020 --> 00:00:57,580
to go and open that up.

15
00:00:58,250 --> 00:01:03,500
Now before we worry about reloading levels and things like that, let's just set up the transitions

16
00:01:03,500 --> 00:01:06,740
between our quiz canvas and our win canvas.

17
00:01:06,890 --> 00:01:10,570
So let's go ahead and tidy up the comments above starts and update.

18
00:01:10,580 --> 00:01:14,480
But we are going to need both of these methods, so we'll leave them intact for now.

19
00:01:15,020 --> 00:01:17,630
Up at the top, we're going to need a few variables.

20
00:01:17,630 --> 00:01:23,180
We're going to need access to our quiz script so we can say quiz and we'll call it the quiz.

21
00:01:23,970 --> 00:01:26,430
And we need access to our end screen script.

22
00:01:26,430 --> 00:01:28,320
So we will call this the end screen.

23
00:01:29,010 --> 00:01:32,460
And in start, let's go ahead and find both these objects.

24
00:01:32,610 --> 00:01:37,890
So our quiz is going to be equal to find object of type quiz.

25
00:01:39,390 --> 00:01:45,150
And our end screen is going to be equal to find objects of type end screen.

26
00:01:46,480 --> 00:01:52,300
Next, we're going to want to toggle the visibility of these canvases so that our quiz canvas is visible

27
00:01:52,300 --> 00:01:55,180
when the game starts and our end screen is hidden.

28
00:01:55,270 --> 00:01:58,720
And then later on in the game's lifecycle, these will swap over.

29
00:01:58,960 --> 00:02:10,509
So also in start, let's say that our quiz game object dot set active equals true and our end screen

30
00:02:10,930 --> 00:02:15,400
game object dot set active equals false.

31
00:02:16,090 --> 00:02:23,010
So what this is doing is taking the game object itself and then changing its active state in the hierarchy.

32
00:02:23,020 --> 00:02:28,880
So if we jump back to unity, it's just like toggling the little flag next to the name up in the inspector.

33
00:02:28,900 --> 00:02:30,910
So if it's true, we can see it.

34
00:02:30,910 --> 00:02:32,890
And if it's false, it'll disappear.

35
00:02:33,280 --> 00:02:38,380
While we're here in our hierarchy, let's enable both our quiz canvas and our win canvas.

36
00:02:38,410 --> 00:02:43,480
It makes things look a bit messy, but that's okay because if we go ahead and hit play, then our game

37
00:02:43,480 --> 00:02:49,060
manager is going to sort everything out for us and it's going to enable our quiz canvas and disable

38
00:02:49,060 --> 00:02:50,200
our win canvas.

39
00:02:50,470 --> 00:02:55,960
Next, our game manager is going to want to toggle the states for our two canvases when the game ends.

40
00:02:56,200 --> 00:03:02,620
So back over in our script, we're going to do this in update and this is going to be your challenge.

41
00:03:02,740 --> 00:03:09,640
I would like you to check whether the game has been completed and if it has, then we want to disable

42
00:03:09,640 --> 00:03:13,110
the quiz canvas and enable the win canvas.

43
00:03:13,120 --> 00:03:15,820
So pause the video now and give that a go.

44
00:03:21,090 --> 00:03:21,570
Okay.

45
00:03:21,570 --> 00:03:22,530
Welcome back.

46
00:03:22,560 --> 00:03:26,640
So to check whether the game is completed, we're going to need an if statement.

47
00:03:26,850 --> 00:03:32,640
And if you recall in our quiz script, we have a public variable called is complete.

48
00:03:33,150 --> 00:03:37,710
And this was being set when the progress bar was completely filled.

49
00:03:38,160 --> 00:03:47,400
So if our game is complete, we want to take our quiz game object and change the set active to false.

50
00:03:48,480 --> 00:03:50,400
And we want to take our end screen.

51
00:03:50,700 --> 00:03:52,050
Dot game object.

52
00:03:52,230 --> 00:03:54,810
Dot set active to true.

53
00:03:55,980 --> 00:04:00,300
Finally, for this method, we're going to want a way of reloading our scene.

54
00:04:00,300 --> 00:04:06,450
If the player clicks the play again button and we can do this using the unity scene management system.

55
00:04:06,450 --> 00:04:10,380
So at the top of our script, let's add another using statement.

56
00:04:10,920 --> 00:04:15,990
And this time we're going to use unity engine dot scene management.

57
00:04:16,870 --> 00:04:20,740
We can then create a new method that we can attach to our button.

58
00:04:20,860 --> 00:04:26,800
So let's create a public void and we'll call this on replay level.

59
00:04:29,240 --> 00:04:30,950
And to reload the scene.

60
00:04:30,950 --> 00:04:38,840
We want the scene manager to load scene and in here we have a multitude of options and if we scroll

61
00:04:38,840 --> 00:04:45,110
through, we're going to use this option here where we pass in the scene, build index and to get the

62
00:04:45,110 --> 00:04:53,540
build index of the current scene, we can say scene manager dot get active scene dot build index.

63
00:04:54,410 --> 00:05:00,290
So this is getting the build index of the currently active scene and we're just telling the scene manager

64
00:05:00,290 --> 00:05:02,300
to load that scene again.

65
00:05:02,630 --> 00:05:07,910
That will refresh our quiz and all of our scores and allow the player to play the game again.

66
00:05:08,210 --> 00:05:15,290
So let's go ahead and save our game manager script, jump back into unity and on our win canvas we want

67
00:05:15,290 --> 00:05:22,220
to go to our replay button, scroll down and in our on click event we're going to add the plus icon.

68
00:05:23,120 --> 00:05:25,340
Drag over our game manager.

69
00:05:26,090 --> 00:05:32,630
And then in the dropdown we're going to select the game manager and then on replay level, now it's

70
00:05:32,630 --> 00:05:37,810
finally time to test everything out and check that everything our game is working correctly.

71
00:05:37,820 --> 00:05:39,770
So let's go ahead and hit play.

72
00:05:40,710 --> 00:05:45,960
And we find that after we've answered all of our questions, our windscreen is popping up and our quiz

73
00:05:45,960 --> 00:05:47,460
canvas is being hidden.

74
00:05:47,460 --> 00:05:50,220
But our score message has not been updated.

75
00:05:50,220 --> 00:05:53,310
And that is because we've forgotten to call the method that does that.

76
00:05:53,700 --> 00:05:57,090
But what we're hearing is at least double check our play again button.

77
00:05:57,270 --> 00:05:59,360
And the game does seem to reload.

78
00:05:59,370 --> 00:06:04,770
But we've also got a warning message down here and it's saying we've got a problem with our display

79
00:06:04,770 --> 00:06:06,690
answer on line 78.

80
00:06:06,690 --> 00:06:09,120
So let's look into both of those problems.

81
00:06:09,270 --> 00:06:16,500
Head over into our game manager script first, because when the game is completed, as well as setting

82
00:06:16,500 --> 00:06:24,750
our end screen to be active, we also want to take our end screen dot show final score that should update

83
00:06:24,750 --> 00:06:30,570
that text field for us to show us our final score and for the error that's showing up when we reload

84
00:06:30,570 --> 00:06:33,480
the game, let's head over to our quiz script.

85
00:06:33,720 --> 00:06:39,300
And the problem exists in our update method where we're currently checking to see if we should be loading

86
00:06:39,300 --> 00:06:40,470
an exit question.

87
00:06:40,470 --> 00:06:42,810
And if we are, then we need to get the next question.

88
00:06:42,960 --> 00:06:47,100
Otherwise we're going to be displaying the answer and setting the button state.

89
00:06:47,340 --> 00:06:52,740
And this error is popping up because on the very first update loop of the game, this statement is currently

90
00:06:52,740 --> 00:06:55,020
false and this statement is true.

91
00:06:55,020 --> 00:07:00,180
So we're trying to display an answer, but we haven't actually set a current question yet.

92
00:07:00,570 --> 00:07:06,180
Now this bug can take a little while to track down, but I found the quickest and easiest way to fix

93
00:07:06,180 --> 00:07:12,960
this problem is to go up to where we're creating our has answered early boolean check and just setting

94
00:07:12,960 --> 00:07:18,900
this to true to start with this means that on the very first loop, if we head back down to our update

95
00:07:18,900 --> 00:07:22,050
loop, this check is going to fail.

96
00:07:22,170 --> 00:07:24,690
So we're not going to try and display an answer.

97
00:07:24,690 --> 00:07:30,930
Then on the next update loop, this will be true and everything will work as intended while we're here.

98
00:07:30,930 --> 00:07:36,000
You may have also noticed during testing that when we answer the final question, rather than giving

99
00:07:36,000 --> 00:07:40,530
us time to check the answer, it just jumps straight to our windscreen.

100
00:07:40,860 --> 00:07:45,150
And this is to do with where we're setting our is complete flag.

101
00:07:45,150 --> 00:07:51,000
So we're currently doing it when we select the answer, which is automatically throwing us to that windscreen.

102
00:07:51,000 --> 00:07:54,330
So if instead we cut this out of here for now.

103
00:07:55,040 --> 00:08:00,980
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.

104
00:08:01,250 --> 00:08:05,750
So now when we answer the final question, we're going to wait for that timer to run down so we can

105
00:08:05,750 --> 00:08:06,800
review the answer.

106
00:08:06,950 --> 00:08:12,110
And then in update, it's going to try and load a new question, which isn't going to exist in our list,

107
00:08:12,110 --> 00:08:17,050
but we're going to catch that with our if statement we can say is complete to true.

108
00:08:17,060 --> 00:08:22,310
And then to avoid getting another question and potentially causing an error, let's just return out

109
00:08:22,310 --> 00:08:23,870
of our update loop early.

110
00:08:24,230 --> 00:08:30,620
Finally, before we test everything out for one last time, there's also another bug that you may see

111
00:08:30,830 --> 00:08:33,679
and it's again a null reference exception error.

112
00:08:33,679 --> 00:08:36,370
And this time is to do with the end screen.

113
00:08:36,380 --> 00:08:42,140
But since this bug can show up all over the place, let's tackle it on all of the scripts we've written.

114
00:08:42,289 --> 00:08:46,880
And the problem comes from finding objects in START.

115
00:08:47,180 --> 00:08:51,920
Now to explain this further, let's have a look at the Unity script execution order.

116
00:08:52,190 --> 00:08:56,990
We already know that START will be called when the game starts and before the update loop.

117
00:08:56,990 --> 00:09:02,630
But as you can see from this flowchart, there's also a few more options that are available even before

118
00:09:02,630 --> 00:09:03,380
start.

119
00:09:03,590 --> 00:09:07,190
And the method that we're interested in here is the awake method.

120
00:09:07,490 --> 00:09:12,410
Just like Start Awake is only ever called once for the lifetime of the game object.

121
00:09:12,410 --> 00:09:17,600
And it's good practice whenever we're finding objects in our scene or getting components set up for

122
00:09:17,600 --> 00:09:23,630
our scripts that we do these things in a wake because all of the scripts will run their awake method

123
00:09:23,630 --> 00:09:25,640
and then call their start methods.

124
00:09:25,940 --> 00:09:32,330
So if we jump back over into our script, what we're going to do is whenever we're finding objects in

125
00:09:32,330 --> 00:09:35,960
START, which is going to change our start method to awake.

126
00:09:36,230 --> 00:09:41,990
So in our end screen script, we're going to change start to awake so that our scorekeeper can be grabbed

127
00:09:41,990 --> 00:09:44,900
nice and early in our quiz script.

128
00:09:44,900 --> 00:09:50,240
We're doing some other stuff like setting up progress bars, but because these are related to the quiz

129
00:09:50,240 --> 00:09:53,540
script, it doesn't matter if these also happen in a week.

130
00:09:53,540 --> 00:09:57,770
So let's go and change our start method to awake in here as well.

131
00:09:58,860 --> 00:09:59,730
In Scorekeeper.

132
00:09:59,730 --> 00:10:04,750
We don't have a start method at all so we can leave that as is in our game manager.

133
00:10:04,770 --> 00:10:11,340
We're finding some objects of type and setting things to active, and this is a situation where a start

134
00:10:11,340 --> 00:10:13,590
and an awake method will come in useful.

135
00:10:13,800 --> 00:10:16,680
So above start, let's create an awake method.

136
00:10:20,000 --> 00:10:24,410
In a wake, we're going to cut and paste our finding objects of type.

137
00:10:25,610 --> 00:10:26,510
And then install it.

138
00:10:26,510 --> 00:10:29,510
We're going to set whether these objects are active or not.

139
00:10:30,110 --> 00:10:36,110
So with that aside on script execution order out of the way, let's go ahead and save all of our scripts

140
00:10:36,110 --> 00:10:37,640
and jump back into unity.

141
00:10:38,240 --> 00:10:39,890
Let's go ahead and hit play.

142
00:10:40,400 --> 00:10:43,850
We can once again see that our win canvas is being hidden.

143
00:10:44,180 --> 00:10:50,510
And if we go ahead and jump through our quiz very quickly, we'll find that our quiz canvas becomes

144
00:10:50,510 --> 00:10:55,130
hidden after answering the last question and our win canvas becomes active.

145
00:10:55,160 --> 00:11:01,850
Our score is correctly being updated and displayed, and if we go ahead and play again, then the game

146
00:11:01,850 --> 00:11:06,080
will reload and we'll get no errors at any stage during our game.

147
00:11:06,230 --> 00:11:10,750
So that now brings us to the end of the section and we now have a working game.

148
00:11:10,760 --> 00:11:15,920
The only thing left to do is to add as many questions as you want and give yourselves a big pat on the

149
00:11:15,920 --> 00:11:18,290
back for finishing your quiz master game.

