In this lecture, we're finishing off our functionality so that if I reach my red finishing line or
if I have a crash bonk and bump my head, I reload the scene and take the player right back to the start
of where we started.
This is me crossing the finishing line and that also reloads our scene.
We're going to be talking about namespaces, having a bit of a conversation about the scene manager
class in unity.
Let's get started to best.
Describe what we're doing.
I've got a little bit of theory for us to work through here, so I'm going to talk about organizing
our code.
And the important thing to know is that the more code we have, the higher likelihood of conflicts between
the names and the behaviors that we're creating, especially if you're working on a multi person project.
So for example, programmer, one might be working on the player and they're using movement method,
but player two is working on enemy and also uses movement methods.
So you can have this conflict of naming if you don't have things separated properly so that the movement
in the player class or the player script or wherever programmer one is working on player can't actually
impact or communicate with or know about the movement method that Programmer two is working on.
So in C sharp, our code is grouped with a particular structure to reduce conflicts.
And let me show you how this is broadly organized.
It's kind of like a Russian doll where things stack inside other things.
So we have statements and these are within methods.
So the methods is where we're saying, do this particular thing.
We've done a bunch of these in our code so far, making our methods and also calling methods that unity
has created for us.
Add talk, for example, is a method that adds talk to our object and has it spin, and then methods
are contained within classes.
Or saying this another way Classes are a way to group methods and statements and variables into logical
grouping.
So we put all of that into a particular class and you can see we so far in our project right now we
have a crash detector class, a finish line class and a player controller class.
And usually we have one class per script.
You don't have to you can have multiple classes.
You see here we've got the keyword class and this class is called crash detector.
And over in finish line we have the keyword class and this is called finish line.
So this is the class.
And grouped within that we have our methods and statements and all that kind of stuff.
And so this is us making our own classes, we're making brand new classes, whereas when we call a method
that's already within unity, we're tapping into Unity's classes which are in the big library of classes
that we've got access to.
So this is us making our own, but we can also use the unity ones.
And then classes are grouped in namespaces.
Namespaces will have a bunch of classes, classes have a bunch of methods and so on.
And so if we have a look in here, the using is the keyword that we use to say that it's a particular
namespace.
So a lot of the code we're using is just within the general unity engine namespace.
So if I was to go and comment out using Unity Engine, what would happen in here?
Would we have any conflicts?
Okay.
So mono behavior collider to DX debug.
These are the some of the things that are included within this unity engine namespace.
So we'll take that away.
Or as you can see, these two up at the top are grayed out and then ones that when we make a script,
it just pops them in there saying, Hey, we think you might want to use these, but we don't actually
use them.
So we could delete them if we wanted to.
I'm going to leave them there for now.
Just something we tend to forget about.
They're just there because they're there.
But if we wanted to go and use functionality, that's from a namespace that we don't have here just
yet, we need to use using.
So I'm going to do this within my finish line script.
I'm going to add in using Unity Engine Dot and you can see here's a whole bunch of things that we can
get access to in here.
But I'm looking for scene management and why not just write out using all of the namespaces?
So using this, using that, using that, using that, well, it makes our code very messy and unreadable.
If we come in here and we see that there's using unity engine like, okay, most things use unity engine
call using scene management.
Okay, this relates to scene management.
We're not wading through 18 or 100 or 200 different namespaces in here.
So the philosophy is we only use what we need to and we only tell our code to look at what we want it
to look at.
Okay.
So we're looking at scene management.
What can we do in here?
Well, instead of debug log, we now have access to if I type in scene manager, this is a class that
is within the scene management namespace.
So the scene management manager, class dot and the dot operator allows us to tap into the methods that
are within this class.
And in here I'm going to look for the load scene method and load scene does what you think it will do
at loads a scene.
I'm going to add my parentheses in here and semicolon and I'm going to get to that in a moment.
We've got the squiggly line because it says, oh, I don't know what you want to load.
We'll get to that in just a second.
But I want what I want to show you is the documentation in Unity on scene manager.
So if you type in Unity Docs scene manager into your browser, I've also given you a link for this,
but.
Generally if you search for Unity Docs and then whatever it is you're interested in, you'll find pretty
quickly the scripting API scene manager dash unity and this will give us some extra information.
So within here, the scene manager, we haven't looked at this before, but this is quite important.
There's lots of cool information in here.
We can see that the scene manager class, which is in the Unity Scene Management namespace, you can
see that it has some static properties that's is seen manager class has some static methods and it has
some events.
So you don't need to know exactly the difference between static and not static and what events are.
But you can see that we've got some methods in here that we can access and use.
So we can use load scene, we can use merge scenes, we can use create scene or get active scene.
So these are the things that are built in already.
So if you're wondering how do we know the list of stuff when you go to the Unity Docs and you can see
in classes within the Unity Scene management, we've got a few different classes within this particular
class.
We've got a bunch of different methods.
So I just want to show you that Unity has got great documentation for when you start to get more comfortable
and you want when you want to dig into these particular things.
And if we click on Load Scene, what does it tell us in here gives us some information.
It shows us that we need the scene name or the scene build index and it gives us a little bit of a description
of what we're doing.
And then it's going to give us some example code.
So often when you get stuck, you can come in to here and you can find some clues about what's going
on.
But what I know we need to do back into unity.
We need to jump up to file and go to build settings because we need to identify that our scene is a
particular scene.
And just to make this even clearer, what I'm going to do is rename my scene.
If we're going to our scenes folder here, you can see it's called sample scene.
It's going to go to file save as I'm going to save this scene in the scenes folder as just say level
one because we've only got one level, we'll probably only have one level for this game, but we'll
call it level one.
Now you see we've got level one and sample scene and they are exactly the same.
So because of that I'm going to delete sample scene, delete, don't need it anymore.
Level one is exactly the same.
So I did a save as and now when I jump up to file build settings you'll see that sample scene has been
deleted.
That's cool.
I wanted to get some change in here, so I'm going to highlight that hit delete.
So it's cleaned up.
Now what I need to do is add open scenes and it will put in my level one.
So that's the process for adding a new scene into build index.
And if you go and make a whole bunch of scenes in here and you don't add them within your build settings,
then Unity won't know about them and won't include them within your game and won't allow the scene manager
to load between them.
So now that we know this is level one, so we know the name is Level one and we know the index all the
way over here is zero and is often the case with programming.
We start at zero and then we go to one and two and three.
So this is scene index zero.
If I jump back over into load scene, we can either put in here the name or we can put in the index.
For now, I'm just going to add zero.
That's the index for this one and only scene we have in our game.
So when the player goes through the finishing line, we are going to load scene zero.
Let's save on that and see if we've got our functionality correct.
It's going to close down the build settings.
We don't need to do anything more in here, but in the future this is where we would build so we can
make a WebGL build.
For example, I'm going to click on play and that was the finish line I was working on.
So when we scroll down here, it puts me all the way back to the start.
So I use reloading the whole scene.
It's the equivalent of me pushing play is what's going on there, because our code is saying, just
reload the scene and we don't have a delay.
There's a little bit of fanciness we can do around this.
We'll do later on.
But now this is doing exactly what we wanted it to, which is just reloading the exact same scene,
putting us back to the start of the level.
So I have a challenge for you.
You've probably guess what it is already.
It's to reload our scene when we bump into the ground to finish the crash detector script that you're
working on in the last lecture.
So pause the video and implement that functionality.
Okay.
Let's find the one we're looking for.
We're looking for crash detector in here instead of debug log out.
I hit my head.
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
trying to do squiggly line it's not auto completing that's because we're not using the unity engine
scene management little trick for you here if you type in scene manager this is we you and I know this
is from the scene management namespace.
If I hit control and period or full stop on the keyboard, it gives me some options in here and it gives
me the option of adding to our script using unity engine scene management.
So I can just click on that and it will automatically add in the namespace that's relevant for the thing
I'm typing in, so that's a quick way of doing it.
Or you could have just typed out the whole using statement at the top here.
Okay, scene manager load scene and it's going to be build index zero once again save with our semicolon
at the end, click on play.
Actually, you know what?
I'm going to move my finish line a little bit further away just so we don't crash into it before I've
had a chance to crash into the ground bump down there.
And we will get rolling.
And then if I flip upside down and we reload the scene.
Boink.
Excellent point.
So we've got that working.
We've got our win condition.
If we make it all the way down to our red thingamajig, be our finishing line, then we'll reload the
scene.
And also, if we bonk our head, we reload the scene using the scene manager class, which is contained
within the scene management namespace.
Great work.
I'll see you in the next lecture.