In this lecture, we're going to be making a level manager to allow us to connect up our three different
scenes.
Now the first step in doing this is to do a little bit of extra set up in unity.
So if we head up to file and then down to build settings, we can see at the top here we've got this
scenes in our build file.
By default, this will already include the scene that came with creating the project itself.
This is usually called sample scene, but remember we renamed ours to Gain and we can see in this list
we have the same name on the left and we also have a build index on the right.
Now, since we've added some additional scenes for our main menu and our game over screen, we're going
to need to add these to our build queue so we can do this by just dragging over the scenes into this
list and we can see that over on the right.
They've been given their own unique build index.
Our scenes aren't currently in the correct order for us though, because when we start a unity game,
once we've built everything, it will always load the index 0/1.
So we're going to need to reorder the elements of our list and put the main menu first.
That's incredibly easy.
We just need to click and drag.
Once we've done this, we'll notice that the build indexes have all changed.
With that set, we can now close this window down.
We don't have to hit any of the buttons at the bottom, just the X at the top.
And now let's create a new level manager.
So over in the hierarchy, this is going to be a game object that exists in the world.
So let's create an empty object called Level Manager and we'll reset the transform.
Then over in our scripts folder, let's create a level manager script.
So right click, create new C-sharp script and call this the level manager and then we'll drag that
onto our game object.
Finally, let's open this script up and start doing some coding for this script.
We're not going to need our start and our update method, but what we are going to need is at the very
top of our script, we're going to have to include using unity engine scene management and that will
allow us to start loading scenes in our script with that set.
Now let's look at how we're going to load our new scene in code.
And to start with, let's look at loading the game scene so we can attach that to the play button on
our main menu.
So we're going to want some public methods.
These are not going to return anything and we'll call this first one load gang in here.
We want to take our scene manager, which comes with our scene management package, dot load scene.
And if we take a look at the tooltip, we'll see that there's a whole bunch of overloaded versions of
this method.
However, it really comes down to whether you want to load your scene by name or by build index.
So we have two real options there.
So to load our game scene, we may want to pass in the build index of one because remember it was the
second item in our scene list, or we could reference it by name so we could just write in the name
of the scene for simplicity and readability of our code.
I think we'll stick with the string reference for now and that is pretty much the end of this method,
which I think would bring us nicely on to your first challenge.
I would like you to write a couple of extra public methods.
The first one will load the main menu and the second will load the game over scene.
So pause the video now and give that go.
Okay.
Welcome back.
So hopefully you got on well with that first challenge.
So for our first method, we can make a public void called load main menu.
And this is going to take our scene manager load scene of our main menu.
Then for our second method, we want a public void called load.
Game over and this will take our scene manager load scene of our game over scene.
So hopefully that was pretty straightforward.
But before we finish up our level manager script, there's one more thing we need to do in here.
Remember that on our main menu we also have a quit button.
So let's add that functionality into this script as well.
For this, we can have another public void method called Quit Game and quit out of a unity game.
We need to say application dot quit.
Now this is a pretty hard way of quitting out of an application and depending on your platform, it
may or may not work as intended.
If you've got a normal standalone build for your game, then this should be fine.
But if you're using a WebGL build then application quit.
Won't do anything because you can't quit out of an embedded web page.
And if you're working on mobile, then there may be a couple of other things you want to do in here
before just hard exiting the application.
And because we won't see this working in our editor, let's also add a debug log in here just to let
us know that we're quitting the game.
So with that set, let's save our script and jump over into Unity.
And now let's start thinking about how to connect this level manager up.
Well, the first thing we probably want to do is prefab our level manager, because this is going to
have to exist in all of our different scenes, kind of like our audio player is at the moment.
So let's drop that into our prefabs folder and then let's connect our start and our quit button on our
main menu.
If we click on our buttons and scroll down, we can add an on click event.
So let's add the plus icon.
To add an event, let's drag over our level manager.
And then in the dropdown menu we can click on level manager and then load game.
And for our quit button it's very similar.
We're going to add an on click event, drag over our level manager and this time we're going to go to
level manager and then quit game.
So before we set up the other scenes in our game, let's click on play just to see this in action.
So if we look at our console and hit the quit button, then we get our quitting game message.
And if we hit the start button, then we load into our game.
Now, from here, nothing is going to happen.
So when we die, we're just going to die.
So the next steps are going to be to call our level manager from our health script when our player dies
and also connect up our game over screen.
So let's look at our health script first.
And since we're going to need to add a level manager to our game scene, let's load that up.
Remember to save your scene before changing.
And with that loaded, let's drag in our level manager and then open up our health script.
In here.
We're going to start by finding a reference to our level manager.
So let's say level manager and we'll call it the level manager.
In awake we can find our object in the scene.
So we have level manager equals find object of type level manager.
And then down in our die method where we're processing what happens when something dies, we're checking
if we're not the player and then modifying our school.
But if we are the player, so else.
We won't say level manager.
Load game over.
And now when we run out of health, it will load us into our game over menu.
This now brings us onto a second challenge for you, and that is to connect the menu buttons in the
game overseen.
So you take what we learnt from connecting up the main menu and apply it to our game over menu.
Pause the video now and give that a go.
Okay.
Welcome back.
So here I am in our game.
The first thing I want to do is drag over my level manager into the scene and then on my canvas.
We just need to go to our button group, click on our replay button, scroll down to our on click event,
add a new event, drag our level manager over, and then in the dropdown we want level manager dot load
game because we want to reload the game scene.
And then on our main menu button, same process.
So add an event, drag over our level manager and then in our dropdown we want to load the main menu.
So with that all set up, our game should have a nice flow between the different states of our game
if we jump back to our main menu scene.
So we can start from the beginning.
If we go ahead and hit play, we can now start our game and play as we normally would.
And if we take enough damage and die, then we load into our game overseeing and we can either hit our
main menu or play again.
And I would encourage you to go through all of the different combinations to make sure everything's
connected up.
But one problem with this scene is that our score is not being transferred over to our game scene.
That's still on our to do list to fix and listen to the music.
When we change our scene, it skips and starts again and it would be really nice if it would continuously
play throughout those scene transitions.
The final problem that we're going to fix in this particular lecture is that when we die, we instantly
get teleported to the game over screen, and it would probably be nice to have a little moment of pause
before that happens.
So let's come out of play mode.
Let's go into our level manager and open up our script.
And I want you to have a think about how we might delay loading this game over.
Okay.
So if you answered a CO routine, then you would be right.
So let's add a CO routine to our level manager so we can delay the loading of this game over at the
bottom.
Let's create an AI enumerator for our CO routine and we'll call this weight and load and to make this
as generic as possible.
In case we want to add more scenes with a load delay, let's pass in a string reference for our scene
name and a float for the amount of delay we want to wait.
And this method is really going to be as simple as waiting for the delay time and then loading the new
thing so we can say yield return new wait 4 seconds and the seconds we want to wait is the delay.
And then once that time has elapsed, we can take our scene manager load scene and we want to load our
scene name.
So with that set, let's change our load game over.
And rather than calling load seen here, let's call, start counting, wait and load passing in the
scene name and then rather than hard coding of value in here, let's pass in a variable name.
So add this to the top of our script in just a moment, but let's call this one scene load delay.
This is going to give us some red squiggly because our variable doesn't exist yet.
So let's copy this name and go right to the top.
And let's create a serialized field of type float and call it the scene load delay.
And let's maybe give this a default value of 2 seconds.
We're still getting a red squiggly and well done if you've spotted that.
I've missed a closing parenthesis.
So now let's go back to Unity and test this out.
And if we click on our level manager, we can now see we've got this variable available to us now to
be very careful editing this value because it does only apply to this version of the level manager.
So rather than changing things in here, it would be a better option to probably change the prefab version
instead.
But with that warning aside, let's go ahead and hit play and test things out.
We can hit our start button to load our game and if we die from our enemies, then we'll have a short
delay before loading into our game overseeing.
So congratulations on hooking up your level manager.
We now have a very simple way of transitioning between our various scenes with that set.
The next problem we're going to tackle is to get our audio playing continuously throughout the different
scene transitions.
We'll start that in the next lecture, so I'll see you there.