1
00:00:04,160 --> 00:00:09,500
In this lecture, we're going to be making a level manager to allow us to connect up our three different

2
00:00:09,500 --> 00:00:10,190
scenes.

3
00:00:11,390 --> 00:00:15,940
Now the first step in doing this is to do a little bit of extra set up in unity.

4
00:00:15,950 --> 00:00:22,550
So if we head up to file and then down to build settings, we can see at the top here we've got this

5
00:00:22,550 --> 00:00:24,460
scenes in our build file.

6
00:00:24,470 --> 00:00:29,440
By default, this will already include the scene that came with creating the project itself.

7
00:00:29,450 --> 00:00:35,450
This is usually called sample scene, but remember we renamed ours to Gain and we can see in this list

8
00:00:35,450 --> 00:00:41,300
we have the same name on the left and we also have a build index on the right.

9
00:00:41,570 --> 00:00:47,030
Now, since we've added some additional scenes for our main menu and our game over screen, we're going

10
00:00:47,030 --> 00:00:53,270
to need to add these to our build queue so we can do this by just dragging over the scenes into this

11
00:00:53,270 --> 00:00:56,180
list and we can see that over on the right.

12
00:00:56,180 --> 00:00:58,850
They've been given their own unique build index.

13
00:00:59,090 --> 00:01:03,980
Our scenes aren't currently in the correct order for us though, because when we start a unity game,

14
00:01:03,980 --> 00:01:08,180
once we've built everything, it will always load the index 0/1.

15
00:01:08,180 --> 00:01:13,040
So we're going to need to reorder the elements of our list and put the main menu first.

16
00:01:13,070 --> 00:01:14,420
That's incredibly easy.

17
00:01:14,420 --> 00:01:16,760
We just need to click and drag.

18
00:01:16,760 --> 00:01:21,170
Once we've done this, we'll notice that the build indexes have all changed.

19
00:01:21,410 --> 00:01:23,930
With that set, we can now close this window down.

20
00:01:23,930 --> 00:01:27,890
We don't have to hit any of the buttons at the bottom, just the X at the top.

21
00:01:28,130 --> 00:01:30,500
And now let's create a new level manager.

22
00:01:30,500 --> 00:01:34,940
So over in the hierarchy, this is going to be a game object that exists in the world.

23
00:01:34,940 --> 00:01:40,370
So let's create an empty object called Level Manager and we'll reset the transform.

24
00:01:40,790 --> 00:01:44,660
Then over in our scripts folder, let's create a level manager script.

25
00:01:44,660 --> 00:01:50,690
So right click, create new C-sharp script and call this the level manager and then we'll drag that

26
00:01:50,690 --> 00:01:52,250
onto our game object.

27
00:01:52,250 --> 00:01:56,630
Finally, let's open this script up and start doing some coding for this script.

28
00:01:56,630 --> 00:02:02,150
We're not going to need our start and our update method, but what we are going to need is at the very

29
00:02:02,150 --> 00:02:09,560
top of our script, we're going to have to include using unity engine scene management and that will

30
00:02:09,560 --> 00:02:13,430
allow us to start loading scenes in our script with that set.

31
00:02:13,430 --> 00:02:16,910
Now let's look at how we're going to load our new scene in code.

32
00:02:16,910 --> 00:02:22,010
And to start with, let's look at loading the game scene so we can attach that to the play button on

33
00:02:22,010 --> 00:02:22,940
our main menu.

34
00:02:22,940 --> 00:02:25,100
So we're going to want some public methods.

35
00:02:25,310 --> 00:02:30,980
These are not going to return anything and we'll call this first one load gang in here.

36
00:02:30,980 --> 00:02:37,610
We want to take our scene manager, which comes with our scene management package, dot load scene.

37
00:02:38,030 --> 00:02:42,650
And if we take a look at the tooltip, we'll see that there's a whole bunch of overloaded versions of

38
00:02:42,650 --> 00:02:43,550
this method.

39
00:02:43,550 --> 00:02:49,190
However, it really comes down to whether you want to load your scene by name or by build index.

40
00:02:49,190 --> 00:02:51,230
So we have two real options there.

41
00:02:51,380 --> 00:02:57,320
So to load our game scene, we may want to pass in the build index of one because remember it was the

42
00:02:57,320 --> 00:03:03,740
second item in our scene list, or we could reference it by name so we could just write in the name

43
00:03:03,740 --> 00:03:07,280
of the scene for simplicity and readability of our code.

44
00:03:07,280 --> 00:03:12,650
I think we'll stick with the string reference for now and that is pretty much the end of this method,

45
00:03:12,650 --> 00:03:16,280
which I think would bring us nicely on to your first challenge.

46
00:03:16,940 --> 00:03:20,540
I would like you to write a couple of extra public methods.

47
00:03:20,540 --> 00:03:26,540
The first one will load the main menu and the second will load the game over scene.

48
00:03:26,540 --> 00:03:29,480
So pause the video now and give that go.

49
00:03:34,900 --> 00:03:35,350
Okay.

50
00:03:35,350 --> 00:03:36,050
Welcome back.

51
00:03:36,070 --> 00:03:38,460
So hopefully you got on well with that first challenge.

52
00:03:38,470 --> 00:03:44,470
So for our first method, we can make a public void called load main menu.

53
00:03:46,090 --> 00:03:51,580
And this is going to take our scene manager load scene of our main menu.

54
00:03:52,480 --> 00:03:56,620
Then for our second method, we want a public void called load.

55
00:03:56,620 --> 00:04:04,960
Game over and this will take our scene manager load scene of our game over scene.

56
00:04:05,470 --> 00:04:07,630
So hopefully that was pretty straightforward.

57
00:04:07,630 --> 00:04:12,240
But before we finish up our level manager script, there's one more thing we need to do in here.

58
00:04:12,250 --> 00:04:16,110
Remember that on our main menu we also have a quit button.

59
00:04:16,120 --> 00:04:19,269
So let's add that functionality into this script as well.

60
00:04:19,510 --> 00:04:26,680
For this, we can have another public void method called Quit Game and quit out of a unity game.

61
00:04:26,680 --> 00:04:29,950
We need to say application dot quit.

62
00:04:30,550 --> 00:04:36,040
Now this is a pretty hard way of quitting out of an application and depending on your platform, it

63
00:04:36,040 --> 00:04:38,500
may or may not work as intended.

64
00:04:38,530 --> 00:04:42,520
If you've got a normal standalone build for your game, then this should be fine.

65
00:04:42,520 --> 00:04:46,130
But if you're using a WebGL build then application quit.

66
00:04:46,210 --> 00:04:50,140
Won't do anything because you can't quit out of an embedded web page.

67
00:04:50,140 --> 00:04:54,790
And if you're working on mobile, then there may be a couple of other things you want to do in here

68
00:04:54,790 --> 00:04:57,610
before just hard exiting the application.

69
00:04:57,850 --> 00:05:04,180
And because we won't see this working in our editor, let's also add a debug log in here just to let

70
00:05:04,180 --> 00:05:06,520
us know that we're quitting the game.

71
00:05:07,090 --> 00:05:10,750
So with that set, let's save our script and jump over into Unity.

72
00:05:11,320 --> 00:05:14,860
And now let's start thinking about how to connect this level manager up.

73
00:05:15,160 --> 00:05:19,660
Well, the first thing we probably want to do is prefab our level manager, because this is going to

74
00:05:19,660 --> 00:05:24,520
have to exist in all of our different scenes, kind of like our audio player is at the moment.

75
00:05:24,970 --> 00:05:30,760
So let's drop that into our prefabs folder and then let's connect our start and our quit button on our

76
00:05:30,760 --> 00:05:31,570
main menu.

77
00:05:31,960 --> 00:05:36,550
If we click on our buttons and scroll down, we can add an on click event.

78
00:05:36,790 --> 00:05:38,320
So let's add the plus icon.

79
00:05:38,320 --> 00:05:41,770
To add an event, let's drag over our level manager.

80
00:05:41,770 --> 00:05:46,270
And then in the dropdown menu we can click on level manager and then load game.

81
00:05:46,270 --> 00:05:48,520
And for our quit button it's very similar.

82
00:05:48,520 --> 00:05:53,860
We're going to add an on click event, drag over our level manager and this time we're going to go to

83
00:05:53,860 --> 00:05:56,050
level manager and then quit game.

84
00:05:56,260 --> 00:06:01,660
So before we set up the other scenes in our game, let's click on play just to see this in action.

85
00:06:02,870 --> 00:06:08,060
So if we look at our console and hit the quit button, then we get our quitting game message.

86
00:06:08,510 --> 00:06:11,660
And if we hit the start button, then we load into our game.

87
00:06:11,990 --> 00:06:13,760
Now, from here, nothing is going to happen.

88
00:06:13,760 --> 00:06:16,160
So when we die, we're just going to die.

89
00:06:16,400 --> 00:06:21,590
So the next steps are going to be to call our level manager from our health script when our player dies

90
00:06:21,590 --> 00:06:24,290
and also connect up our game over screen.

91
00:06:24,500 --> 00:06:27,050
So let's look at our health script first.

92
00:06:27,230 --> 00:06:31,460
And since we're going to need to add a level manager to our game scene, let's load that up.

93
00:06:31,580 --> 00:06:34,400
Remember to save your scene before changing.

94
00:06:34,970 --> 00:06:40,430
And with that loaded, let's drag in our level manager and then open up our health script.

95
00:06:41,310 --> 00:06:41,820
In here.

96
00:06:41,820 --> 00:06:45,390
We're going to start by finding a reference to our level manager.

97
00:06:45,480 --> 00:06:49,380
So let's say level manager and we'll call it the level manager.

98
00:06:50,490 --> 00:06:53,670
In awake we can find our object in the scene.

99
00:06:53,670 --> 00:06:58,470
So we have level manager equals find object of type level manager.

100
00:06:59,830 --> 00:07:05,980
And then down in our die method where we're processing what happens when something dies, we're checking

101
00:07:05,980 --> 00:07:08,860
if we're not the player and then modifying our school.

102
00:07:08,860 --> 00:07:11,470
But if we are the player, so else.

103
00:07:12,130 --> 00:07:14,170
We won't say level manager.

104
00:07:14,620 --> 00:07:16,090
Load game over.

105
00:07:16,450 --> 00:07:20,230
And now when we run out of health, it will load us into our game over menu.

106
00:07:20,320 --> 00:07:26,410
This now brings us onto a second challenge for you, and that is to connect the menu buttons in the

107
00:07:26,410 --> 00:07:27,670
game overseen.

108
00:07:27,670 --> 00:07:32,770
So you take what we learnt from connecting up the main menu and apply it to our game over menu.

109
00:07:32,800 --> 00:07:34,930
Pause the video now and give that a go.

110
00:07:40,490 --> 00:07:40,940
Okay.

111
00:07:40,940 --> 00:07:41,820
Welcome back.

112
00:07:41,840 --> 00:07:43,430
So here I am in our game.

113
00:07:44,360 --> 00:07:50,090
The first thing I want to do is drag over my level manager into the scene and then on my canvas.

114
00:07:50,090 --> 00:07:56,780
We just need to go to our button group, click on our replay button, scroll down to our on click event,

115
00:07:57,110 --> 00:08:03,890
add a new event, drag our level manager over, and then in the dropdown we want level manager dot load

116
00:08:03,890 --> 00:08:06,620
game because we want to reload the game scene.

117
00:08:07,470 --> 00:08:10,110
And then on our main menu button, same process.

118
00:08:10,110 --> 00:08:16,470
So add an event, drag over our level manager and then in our dropdown we want to load the main menu.

119
00:08:16,980 --> 00:08:22,440
So with that all set up, our game should have a nice flow between the different states of our game

120
00:08:22,710 --> 00:08:25,050
if we jump back to our main menu scene.

121
00:08:25,050 --> 00:08:26,790
So we can start from the beginning.

122
00:08:27,300 --> 00:08:33,870
If we go ahead and hit play, we can now start our game and play as we normally would.

123
00:08:33,990 --> 00:08:42,960
And if we take enough damage and die, then we load into our game overseeing and we can either hit our

124
00:08:42,960 --> 00:08:44,490
main menu or play again.

125
00:08:44,490 --> 00:08:48,600
And I would encourage you to go through all of the different combinations to make sure everything's

126
00:08:48,600 --> 00:08:49,470
connected up.

127
00:08:49,470 --> 00:08:54,450
But one problem with this scene is that our score is not being transferred over to our game scene.

128
00:08:54,520 --> 00:08:58,020
That's still on our to do list to fix and listen to the music.

129
00:08:58,020 --> 00:09:04,380
When we change our scene, it skips and starts again and it would be really nice if it would continuously

130
00:09:04,380 --> 00:09:06,450
play throughout those scene transitions.

131
00:09:06,720 --> 00:09:12,330
The final problem that we're going to fix in this particular lecture is that when we die, we instantly

132
00:09:12,330 --> 00:09:17,400
get teleported to the game over screen, and it would probably be nice to have a little moment of pause

133
00:09:17,400 --> 00:09:18,780
before that happens.

134
00:09:18,780 --> 00:09:20,490
So let's come out of play mode.

135
00:09:20,520 --> 00:09:23,610
Let's go into our level manager and open up our script.

136
00:09:23,940 --> 00:09:28,950
And I want you to have a think about how we might delay loading this game over.

137
00:09:30,870 --> 00:09:31,530
Okay.

138
00:09:31,530 --> 00:09:35,190
So if you answered a CO routine, then you would be right.

139
00:09:35,310 --> 00:09:42,510
So let's add a CO routine to our level manager so we can delay the loading of this game over at the

140
00:09:42,510 --> 00:09:43,070
bottom.

141
00:09:43,080 --> 00:09:50,580
Let's create an AI enumerator for our CO routine and we'll call this weight and load and to make this

142
00:09:50,580 --> 00:09:51,870
as generic as possible.

143
00:09:51,870 --> 00:09:57,960
In case we want to add more scenes with a load delay, let's pass in a string reference for our scene

144
00:09:57,960 --> 00:10:02,580
name and a float for the amount of delay we want to wait.

145
00:10:03,900 --> 00:10:09,510
And this method is really going to be as simple as waiting for the delay time and then loading the new

146
00:10:09,510 --> 00:10:18,120
thing so we can say yield return new wait 4 seconds and the seconds we want to wait is the delay.

147
00:10:18,570 --> 00:10:25,440
And then once that time has elapsed, we can take our scene manager load scene and we want to load our

148
00:10:25,440 --> 00:10:26,340
scene name.

149
00:10:26,430 --> 00:10:30,270
So with that set, let's change our load game over.

150
00:10:30,300 --> 00:10:37,350
And rather than calling load seen here, let's call, start counting, wait and load passing in the

151
00:10:37,350 --> 00:10:42,690
scene name and then rather than hard coding of value in here, let's pass in a variable name.

152
00:10:42,690 --> 00:10:49,110
So add this to the top of our script in just a moment, but let's call this one scene load delay.

153
00:10:49,650 --> 00:10:53,520
This is going to give us some red squiggly because our variable doesn't exist yet.

154
00:10:53,520 --> 00:10:56,400
So let's copy this name and go right to the top.

155
00:10:56,400 --> 00:11:01,800
And let's create a serialized field of type float and call it the scene load delay.

156
00:11:01,980 --> 00:11:05,760
And let's maybe give this a default value of 2 seconds.

157
00:11:06,000 --> 00:11:09,630
We're still getting a red squiggly and well done if you've spotted that.

158
00:11:09,630 --> 00:11:11,850
I've missed a closing parenthesis.

159
00:11:12,330 --> 00:11:15,270
So now let's go back to Unity and test this out.

160
00:11:15,450 --> 00:11:20,550
And if we click on our level manager, we can now see we've got this variable available to us now to

161
00:11:20,550 --> 00:11:26,760
be very careful editing this value because it does only apply to this version of the level manager.

162
00:11:26,760 --> 00:11:32,790
So rather than changing things in here, it would be a better option to probably change the prefab version

163
00:11:32,790 --> 00:11:33,750
instead.

164
00:11:33,750 --> 00:11:36,960
But with that warning aside, let's go ahead and hit play and test things out.

165
00:11:38,130 --> 00:11:45,780
We can hit our start button to load our game and if we die from our enemies, then we'll have a short

166
00:11:45,780 --> 00:11:48,380
delay before loading into our game overseeing.

167
00:11:48,840 --> 00:11:52,050
So congratulations on hooking up your level manager.

168
00:11:52,080 --> 00:11:57,900
We now have a very simple way of transitioning between our various scenes with that set.

169
00:11:57,900 --> 00:12:03,030
The next problem we're going to tackle is to get our audio playing continuously throughout the different

170
00:12:03,030 --> 00:12:04,320
scene transitions.

171
00:12:04,320 --> 00:12:06,870
We'll start that in the next lecture, so I'll see you there.

