1
00:00:03,130 --> 00:00:07,960
In this lecture, we're finishing off our functionality so that if I reach my red finishing line or

2
00:00:07,960 --> 00:00:13,360
if I have a crash bonk and bump my head, I reload the scene and take the player right back to the start

3
00:00:13,360 --> 00:00:14,230
of where we started.

4
00:00:14,230 --> 00:00:17,890
This is me crossing the finishing line and that also reloads our scene.

5
00:00:17,890 --> 00:00:22,630
We're going to be talking about namespaces, having a bit of a conversation about the scene manager

6
00:00:22,630 --> 00:00:24,160
class in unity.

7
00:00:24,160 --> 00:00:25,300
Let's get started to best.

8
00:00:25,300 --> 00:00:26,110
Describe what we're doing.

9
00:00:26,110 --> 00:00:29,770
I've got a little bit of theory for us to work through here, so I'm going to talk about organizing

10
00:00:29,770 --> 00:00:30,370
our code.

11
00:00:30,370 --> 00:00:35,500
And the important thing to know is that the more code we have, the higher likelihood of conflicts between

12
00:00:35,500 --> 00:00:40,150
the names and the behaviors that we're creating, especially if you're working on a multi person project.

13
00:00:40,150 --> 00:00:46,300
So for example, programmer, one might be working on the player and they're using movement method,

14
00:00:46,300 --> 00:00:50,080
but player two is working on enemy and also uses movement methods.

15
00:00:50,080 --> 00:00:56,170
So you can have this conflict of naming if you don't have things separated properly so that the movement

16
00:00:56,170 --> 00:01:03,100
in the player class or the player script or wherever programmer one is working on player can't actually

17
00:01:03,100 --> 00:01:08,560
impact or communicate with or know about the movement method that Programmer two is working on.

18
00:01:08,560 --> 00:01:13,420
So in C sharp, our code is grouped with a particular structure to reduce conflicts.

19
00:01:13,420 --> 00:01:16,000
And let me show you how this is broadly organized.

20
00:01:16,000 --> 00:01:19,720
It's kind of like a Russian doll where things stack inside other things.

21
00:01:19,720 --> 00:01:22,930
So we have statements and these are within methods.

22
00:01:22,930 --> 00:01:26,380
So the methods is where we're saying, do this particular thing.

23
00:01:26,380 --> 00:01:31,600
We've done a bunch of these in our code so far, making our methods and also calling methods that unity

24
00:01:31,600 --> 00:01:33,220
has created for us.

25
00:01:33,220 --> 00:01:38,800
Add talk, for example, is a method that adds talk to our object and has it spin, and then methods

26
00:01:38,800 --> 00:01:40,930
are contained within classes.

27
00:01:40,930 --> 00:01:49,150
Or saying this another way Classes are a way to group methods and statements and variables into logical

28
00:01:49,150 --> 00:01:49,930
grouping.

29
00:01:49,930 --> 00:01:55,720
So we put all of that into a particular class and you can see we so far in our project right now we

30
00:01:55,720 --> 00:02:00,730
have a crash detector class, a finish line class and a player controller class.

31
00:02:00,730 --> 00:02:03,790
And usually we have one class per script.

32
00:02:03,790 --> 00:02:05,710
You don't have to you can have multiple classes.

33
00:02:05,710 --> 00:02:09,580
You see here we've got the keyword class and this class is called crash detector.

34
00:02:09,580 --> 00:02:13,330
And over in finish line we have the keyword class and this is called finish line.

35
00:02:13,330 --> 00:02:14,560
So this is the class.

36
00:02:14,560 --> 00:02:18,910
And grouped within that we have our methods and statements and all that kind of stuff.

37
00:02:18,910 --> 00:02:26,410
And so this is us making our own classes, we're making brand new classes, whereas when we call a method

38
00:02:26,410 --> 00:02:31,930
that's already within unity, we're tapping into Unity's classes which are in the big library of classes

39
00:02:31,930 --> 00:02:33,310
that we've got access to.

40
00:02:33,310 --> 00:02:36,460
So this is us making our own, but we can also use the unity ones.

41
00:02:36,460 --> 00:02:39,850
And then classes are grouped in namespaces.

42
00:02:39,850 --> 00:02:44,080
Namespaces will have a bunch of classes, classes have a bunch of methods and so on.

43
00:02:44,080 --> 00:02:49,480
And so if we have a look in here, the using is the keyword that we use to say that it's a particular

44
00:02:49,480 --> 00:02:50,110
namespace.

45
00:02:50,110 --> 00:02:54,820
So a lot of the code we're using is just within the general unity engine namespace.

46
00:02:54,820 --> 00:02:59,140
So if I was to go and comment out using Unity Engine, what would happen in here?

47
00:02:59,140 --> 00:03:00,430
Would we have any conflicts?

48
00:03:00,430 --> 00:03:00,940
Okay.

49
00:03:00,940 --> 00:03:04,720
So mono behavior collider to DX debug.

50
00:03:04,720 --> 00:03:08,890
These are the some of the things that are included within this unity engine namespace.

51
00:03:08,890 --> 00:03:10,330
So we'll take that away.

52
00:03:10,330 --> 00:03:15,040
Or as you can see, these two up at the top are grayed out and then ones that when we make a script,

53
00:03:15,040 --> 00:03:18,760
it just pops them in there saying, Hey, we think you might want to use these, but we don't actually

54
00:03:18,760 --> 00:03:19,120
use them.

55
00:03:19,120 --> 00:03:21,370
So we could delete them if we wanted to.

56
00:03:21,370 --> 00:03:22,510
I'm going to leave them there for now.

57
00:03:22,510 --> 00:03:24,010
Just something we tend to forget about.

58
00:03:24,010 --> 00:03:25,390
They're just there because they're there.

59
00:03:25,390 --> 00:03:30,610
But if we wanted to go and use functionality, that's from a namespace that we don't have here just

60
00:03:30,610 --> 00:03:32,560
yet, we need to use using.

61
00:03:32,560 --> 00:03:35,170
So I'm going to do this within my finish line script.

62
00:03:35,170 --> 00:03:44,050
I'm going to add in using Unity Engine Dot and you can see here's a whole bunch of things that we can

63
00:03:44,050 --> 00:03:45,190
get access to in here.

64
00:03:45,190 --> 00:03:50,950
But I'm looking for scene management and why not just write out using all of the namespaces?

65
00:03:50,950 --> 00:03:55,210
So using this, using that, using that, using that, well, it makes our code very messy and unreadable.

66
00:03:55,210 --> 00:03:59,830
If we come in here and we see that there's using unity engine like, okay, most things use unity engine

67
00:03:59,830 --> 00:04:01,540
call using scene management.

68
00:04:01,540 --> 00:04:03,790
Okay, this relates to scene management.

69
00:04:03,790 --> 00:04:08,500
We're not wading through 18 or 100 or 200 different namespaces in here.

70
00:04:08,500 --> 00:04:14,350
So the philosophy is we only use what we need to and we only tell our code to look at what we want it

71
00:04:14,350 --> 00:04:15,010
to look at.

72
00:04:15,010 --> 00:04:15,370
Okay.

73
00:04:15,370 --> 00:04:16,720
So we're looking at scene management.

74
00:04:16,720 --> 00:04:17,709
What can we do in here?

75
00:04:17,709 --> 00:04:26,500
Well, instead of debug log, we now have access to if I type in scene manager, this is a class that

76
00:04:26,500 --> 00:04:28,780
is within the scene management namespace.

77
00:04:28,780 --> 00:04:35,050
So the scene management manager, class dot and the dot operator allows us to tap into the methods that

78
00:04:35,050 --> 00:04:36,400
are within this class.

79
00:04:36,400 --> 00:04:42,370
And in here I'm going to look for the load scene method and load scene does what you think it will do

80
00:04:42,370 --> 00:04:43,420
at loads a scene.

81
00:04:43,420 --> 00:04:48,220
I'm going to add my parentheses in here and semicolon and I'm going to get to that in a moment.

82
00:04:48,220 --> 00:04:50,680
We've got the squiggly line because it says, oh, I don't know what you want to load.

83
00:04:50,680 --> 00:04:51,880
We'll get to that in just a second.

84
00:04:51,880 --> 00:04:56,800
But I want what I want to show you is the documentation in Unity on scene manager.

85
00:04:56,800 --> 00:05:02,080
So if you type in Unity Docs scene manager into your browser, I've also given you a link for this,

86
00:05:02,080 --> 00:05:02,230
but.

87
00:05:02,300 --> 00:05:06,680
Generally if you search for Unity Docs and then whatever it is you're interested in, you'll find pretty

88
00:05:06,680 --> 00:05:11,690
quickly the scripting API scene manager dash unity and this will give us some extra information.

89
00:05:11,690 --> 00:05:15,470
So within here, the scene manager, we haven't looked at this before, but this is quite important.

90
00:05:15,470 --> 00:05:16,940
There's lots of cool information in here.

91
00:05:16,940 --> 00:05:23,840
We can see that the scene manager class, which is in the Unity Scene Management namespace, you can

92
00:05:23,840 --> 00:05:29,150
see that it has some static properties that's is seen manager class has some static methods and it has

93
00:05:29,150 --> 00:05:29,810
some events.

94
00:05:29,810 --> 00:05:33,740
So you don't need to know exactly the difference between static and not static and what events are.

95
00:05:33,740 --> 00:05:37,520
But you can see that we've got some methods in here that we can access and use.

96
00:05:37,520 --> 00:05:42,650
So we can use load scene, we can use merge scenes, we can use create scene or get active scene.

97
00:05:42,650 --> 00:05:44,810
So these are the things that are built in already.

98
00:05:44,810 --> 00:05:49,430
So if you're wondering how do we know the list of stuff when you go to the Unity Docs and you can see

99
00:05:49,430 --> 00:05:55,130
in classes within the Unity Scene management, we've got a few different classes within this particular

100
00:05:55,130 --> 00:05:55,490
class.

101
00:05:55,490 --> 00:05:57,650
We've got a bunch of different methods.

102
00:05:57,650 --> 00:06:02,510
So I just want to show you that Unity has got great documentation for when you start to get more comfortable

103
00:06:02,510 --> 00:06:05,330
and you want when you want to dig into these particular things.

104
00:06:05,330 --> 00:06:08,600
And if we click on Load Scene, what does it tell us in here gives us some information.

105
00:06:08,600 --> 00:06:14,990
It shows us that we need the scene name or the scene build index and it gives us a little bit of a description

106
00:06:14,990 --> 00:06:15,740
of what we're doing.

107
00:06:15,740 --> 00:06:17,990
And then it's going to give us some example code.

108
00:06:17,990 --> 00:06:22,220
So often when you get stuck, you can come in to here and you can find some clues about what's going

109
00:06:22,220 --> 00:06:22,580
on.

110
00:06:22,580 --> 00:06:24,950
But what I know we need to do back into unity.

111
00:06:24,950 --> 00:06:30,770
We need to jump up to file and go to build settings because we need to identify that our scene is a

112
00:06:30,770 --> 00:06:31,730
particular scene.

113
00:06:31,730 --> 00:06:35,300
And just to make this even clearer, what I'm going to do is rename my scene.

114
00:06:35,300 --> 00:06:38,360
If we're going to our scenes folder here, you can see it's called sample scene.

115
00:06:38,390 --> 00:06:44,990
It's going to go to file save as I'm going to save this scene in the scenes folder as just say level

116
00:06:44,990 --> 00:06:48,440
one because we've only got one level, we'll probably only have one level for this game, but we'll

117
00:06:48,440 --> 00:06:49,280
call it level one.

118
00:06:49,280 --> 00:06:52,730
Now you see we've got level one and sample scene and they are exactly the same.

119
00:06:52,730 --> 00:06:56,480
So because of that I'm going to delete sample scene, delete, don't need it anymore.

120
00:06:56,480 --> 00:06:58,020
Level one is exactly the same.

121
00:06:58,070 --> 00:07:04,190
So I did a save as and now when I jump up to file build settings you'll see that sample scene has been

122
00:07:04,190 --> 00:07:04,670
deleted.

123
00:07:04,670 --> 00:07:05,120
That's cool.

124
00:07:05,120 --> 00:07:08,540
I wanted to get some change in here, so I'm going to highlight that hit delete.

125
00:07:08,540 --> 00:07:09,530
So it's cleaned up.

126
00:07:09,530 --> 00:07:14,390
Now what I need to do is add open scenes and it will put in my level one.

127
00:07:14,390 --> 00:07:17,540
So that's the process for adding a new scene into build index.

128
00:07:17,540 --> 00:07:22,550
And if you go and make a whole bunch of scenes in here and you don't add them within your build settings,

129
00:07:22,550 --> 00:07:27,590
then Unity won't know about them and won't include them within your game and won't allow the scene manager

130
00:07:27,590 --> 00:07:29,210
to load between them.

131
00:07:29,210 --> 00:07:35,060
So now that we know this is level one, so we know the name is Level one and we know the index all the

132
00:07:35,060 --> 00:07:38,030
way over here is zero and is often the case with programming.

133
00:07:38,030 --> 00:07:40,220
We start at zero and then we go to one and two and three.

134
00:07:40,220 --> 00:07:42,560
So this is scene index zero.

135
00:07:42,560 --> 00:07:47,120
If I jump back over into load scene, we can either put in here the name or we can put in the index.

136
00:07:47,120 --> 00:07:48,560
For now, I'm just going to add zero.

137
00:07:48,560 --> 00:07:51,980
That's the index for this one and only scene we have in our game.

138
00:07:51,980 --> 00:07:55,940
So when the player goes through the finishing line, we are going to load scene zero.

139
00:07:55,970 --> 00:07:58,970
Let's save on that and see if we've got our functionality correct.

140
00:07:58,970 --> 00:08:00,830
It's going to close down the build settings.

141
00:08:00,830 --> 00:08:04,510
We don't need to do anything more in here, but in the future this is where we would build so we can

142
00:08:04,520 --> 00:08:05,780
make a WebGL build.

143
00:08:05,780 --> 00:08:09,980
For example, I'm going to click on play and that was the finish line I was working on.

144
00:08:09,980 --> 00:08:13,910
So when we scroll down here, it puts me all the way back to the start.

145
00:08:13,910 --> 00:08:16,250
So I use reloading the whole scene.

146
00:08:16,250 --> 00:08:21,200
It's the equivalent of me pushing play is what's going on there, because our code is saying, just

147
00:08:21,200 --> 00:08:23,360
reload the scene and we don't have a delay.

148
00:08:23,360 --> 00:08:25,220
There's a little bit of fanciness we can do around this.

149
00:08:25,250 --> 00:08:25,910
We'll do later on.

150
00:08:25,910 --> 00:08:31,250
But now this is doing exactly what we wanted it to, which is just reloading the exact same scene,

151
00:08:31,250 --> 00:08:32,900
putting us back to the start of the level.

152
00:08:32,900 --> 00:08:34,100
So I have a challenge for you.

153
00:08:34,100 --> 00:08:35,539
You've probably guess what it is already.

154
00:08:35,539 --> 00:08:40,970
It's to reload our scene when we bump into the ground to finish the crash detector script that you're

155
00:08:40,970 --> 00:08:42,500
working on in the last lecture.

156
00:08:42,500 --> 00:08:45,380
So pause the video and implement that functionality.

157
00:08:47,390 --> 00:08:47,780
Okay.

158
00:08:47,780 --> 00:08:49,100
Let's find the one we're looking for.

159
00:08:49,100 --> 00:08:53,120
We're looking for crash detector in here instead of debug log out.

160
00:08:53,120 --> 00:08:53,930
I hit my head.

161
00:08:53,930 --> 00:09:00,320
I'm going to use the same code scene manager and you can see as I type it in it doesn't know what I'm

162
00:09:00,320 --> 00:09:05,870
trying to do squiggly line it's not auto completing that's because we're not using the unity engine

163
00:09:06,170 --> 00:09:12,350
scene management little trick for you here if you type in scene manager this is we you and I know this

164
00:09:12,350 --> 00:09:15,230
is from the scene management namespace.

165
00:09:15,230 --> 00:09:21,560
If I hit control and period or full stop on the keyboard, it gives me some options in here and it gives

166
00:09:21,560 --> 00:09:27,080
me the option of adding to our script using unity engine scene management.

167
00:09:27,080 --> 00:09:32,780
So I can just click on that and it will automatically add in the namespace that's relevant for the thing

168
00:09:32,780 --> 00:09:35,150
I'm typing in, so that's a quick way of doing it.

169
00:09:35,150 --> 00:09:38,510
Or you could have just typed out the whole using statement at the top here.

170
00:09:38,600 --> 00:09:47,540
Okay, scene manager load scene and it's going to be build index zero once again save with our semicolon

171
00:09:47,540 --> 00:09:49,910
at the end, click on play.

172
00:09:50,180 --> 00:09:50,870
Actually, you know what?

173
00:09:50,870 --> 00:09:55,280
I'm going to move my finish line a little bit further away just so we don't crash into it before I've

174
00:09:55,280 --> 00:09:57,800
had a chance to crash into the ground bump down there.

175
00:09:59,210 --> 00:10:02,090
And we will get rolling.

176
00:10:02,090 --> 00:10:06,710
And then if I flip upside down and we reload the scene.

177
00:10:06,740 --> 00:10:07,370
Boink.

178
00:10:07,670 --> 00:10:08,580
Excellent point.

179
00:10:08,600 --> 00:10:09,560
So we've got that working.

180
00:10:09,560 --> 00:10:11,360
We've got our win condition.

181
00:10:11,360 --> 00:10:17,060
If we make it all the way down to our red thingamajig, be our finishing line, then we'll reload the

182
00:10:17,060 --> 00:10:17,330
scene.

183
00:10:17,330 --> 00:10:23,330
And also, if we bonk our head, we reload the scene using the scene manager class, which is contained

184
00:10:23,330 --> 00:10:26,030
within the scene management namespace.

185
00:10:26,060 --> 00:10:26,660
Great work.

186
00:10:26,660 --> 00:10:27,740
I'll see you in the next lecture.

