Welcome back, My favorite game developers.
In this video, we are going to be not
only transitioning to the next level,
we are also going to be transitioning.
They're in a much cooler way.
So now when we walk through the portal,
we don't go immediately.
We wait two seconds and then we go and we can even
adjust this from the level manager as much as we want.
So you can see we have
a central figure now which is the level manager.
We are going to use coroutines,
scene manager, and all of that good stuff.
So let's not waste any more time and let's get started.
Oh, okay, so now we need a level manager because,
okay, It's all fun and games
when the portal is transitioning us.
But when it comes to big games,
we don't want this particular object to be in
control of the way we move through scenes.
It's much better to have the swan determine which level
we want to move through and then
have a more central object,
which is the level manager that will handle
the process of moving us from level to level.
This later on as you grow.
For now, it doesn't have to be like this.
You can simply just use the portal.
The game will work forever.
When it comes to big games,
the actual object doesn't do the job.
You have a more,
let's say a bigger authority,
a bigger head, which is the level
manager that handles that.
So I'm going to right-click create an
empty, reset its position.
And this is going to be the level manager.
You can satisfy wherever you want.
And you can go into the scripts right
here and to the managers,
right-click create C-sharp script.
The level manager.
Hit Enter and wait for it just a little bit
for it to compile
the goal level manager at the scripting here.
And let's go ahead and open it up.
Now, first thing we want to do
is make this level manager and instance.
What is an instance?
The instance will allow us to call upon
this level manager wherever we are and use
what ever valuable as
public and whatever method is
public available to us are created,
make it an instance and then call it,
and you will understand how that works.
So what we can do is first of all,
we're going to make a public static level manager
of that means this variable will be
off type level manager and call this instance.
And then in start,
we are going to set the instance equal to this saved.
Okay, so what is happening right now?
First of all, public,
we know what public is static.
What is static?
Static means that through all of the code,
we will have this the sink.
So whether we call it from the player controller,
from the bullet impact,
from the room manager.
The level manager instance is always
the same throughout when we instantiate the book,
for example, every single bullet is a clone.
Do you remember? So if I run the game,
let me just show you what I mean.
So I shoot a couple of bullets, pause the game.
You can see that each one of them is a clone.
So if I try to access the player bullet controller,
for example, if it's not static,
some variable it will access,
it will be different than,
for example, a variable which is here or here.
But if I create a static variable on the player bullet,
then whichever bullet I access,
the variable which is static,
will be the same.
So static means it's the same throughout all of the gain,
wherever and whenever you access it.
That is very important when it comes to a static.
So it's of type level manager that's
pretty easy to understand and its name is instance.
We can call it whatever we want.
We can call it Michael if you want,
what will keep it as instance.
And then we have this ironically pun intended and what?
We have instance equal this.
This means the actual class.
So we are using
the instance as soon as we start the game.
Instance is this.
So now wherever we are in the code,
we can go ahead and write level manager dot instance,
and access whichever variable which is public obviously,
or method that we have in here.
So next thing we're going to
do is we'll remove the update for now.
Use it will create it again.
And we need to create something
called a serialized field.
Float. And it's going to be the time to load.
So, and it's going to be equal to 2 f.
Why do we create a font load?
What is actually our goal?
Out of all of this, I forgot to tell you.
What we want to do is we want to
delay the transition to the next level,
so we don't want it to happen instantaneously.
We want to have at least two seconds,
so we get to the portal,
we wait two seconds, and
then we traveled on to the next level.
This will just add a bit of us authenticity as you know,
whenever you are playing a game, you don't.
And to report and then immediately start the next level.
You want to have a bit of time,
two seconds maybe rethink your decision,
but it will be over.
And how are we going to do this?
We'll obviously we are going to be using a coating.
So I'm not going to tell you to create
the quarantine right now because you'll need to do with,
I think I do have a challenge
somewhere where you create the coroutine.
So pay attention very closely.
I know called teens are
very hard to learn in the beginning.
Once you get the hang of it, it's extremely easy.
Let's get started. First of all,
because we will be accessing
this co-routine from a different script.
I'm going to write public eye enumerator.
So the coroutine is of type I and numerator.
And we're going to call it the loading next, next level.
And in here we are going to be taking in an argument.
So it's going to be of type string and it's going to be
the next level, just like that.
And in here what we'll do is we want to wait
for the time to load and then load the scene.
So as we said, yield,
return, new, wait four seconds.
And this wait four seconds,
we are going to use the time to load.
How great is that?
And finally, after we wait 42 seconds,
we are going to go into the scene manager.
We add the scene management in
here and now we can access the scene manager.
So did you notice what happened right now?
It didn't allow me to use the scene manager until I
had the criteria telling unity that we are using this.
So Scene Manager, not Unity, excuse me,
the script right here,
this entire file that we are using, seen management.
So Scene Manager, dot load seen.
And as the next scene that we are going to 0 or I mean,
next level that we are going too low. Okay, great.
Now the next thing that we need to do
is we need to go into level exit.
And instead of using the Scene Manager here,
we simply use the instance to start the co-routine.
And honestly, I'm already very tired,
so I think it's better for you to do this instead of me.
So go ahead, start the co-routine.
First of all, from the level of exit script,
Kali, call the level manager and move to the next level.
There is a small trickier you'll need to watch out for.
I'm going to give you a small hint,
which is you can't simply call a co-routine.
You need to start a quality.
So pause the video right now and go start the co-routine.
Oh, okay, welcome back.
So how did you get on with that?
And here I'm going to remove this.
And instead I'm going to say that I'm
going to start a co-routine.
I'm going to use the level manager.
I hope this was not the hardest part if you
did manage to use instance,
I congratulate you, you move
to the next level of game development.
If not, don't worry about it,
it's probably my mistake
because I didn't tell you I didn't give you the hint.
But we are going to be loading next level.
So this method in loading
next level is actually an eye enumerator from
the level manager and it
requires a next level string right here,
as you can see as an argument.
So I'm going to give it to it.
And what should it be?
It should be the level two lot.
Save that. And one very nice thing.
You can see that when we stopped using the scene manager,
the using Unity's engine
is no longer as wide as the Unity engine alone.
So the things that we don't use are a bit grayed out.
So I can actually remove this,
save a bit of memory.
And now the level exit just allocates
or calls the level manager the central head,
and tells it to load the level.
This will come in handy when we have multiple portals.
You'll see, trust me,
it's much better to do it this way.
So save that.
Go back and to our game right here.
Start the game, walked towards the portal.
I'm playing in the scene mode.
So now when I hit the portal,
we should have two seconds before we vote.
And we can see it right here in
London manager time to load two seconds.
So there we go. Here goes nothing.
112 up.
There we go.
So now we are in the level 1
and we didn't go to there immediately.
So I hope you enjoy it.
Make sure to state and all of your changes.
And in the next video we are going to make
the transitions even cooler.
Can you imagine that even cooler than this?
Make sure to leave a review for the cool transitions.
I'll see you then.