In his lecture.
We're hooking up our level portals so we can wait for a moment and then load to the next level.
In its price folder, you should have an exit says exit pixelated.
Yet another piece of amazing Rick Art.
If you jump over in the hierarchy, right click to the object sprites and a square.
You can rename that to be exit.
And then in exit where we're looking for the sprite renderer instead of the Sprite brings square, we
will drag over our exit and then we go for exit pixelated.
We can't see it at the moment because of our sorting layers, so up to layer and we will change the
layer that we need a new layer for these things or can we just put it on something else?
You know, we'll add a new layer.
I don't really like having thousands of layers in here, but in a way it's kind of safer.
And we'll call this interactive walls.
Things that you can bump into is interact bubbles or eyeballs.
Eyeballs that doesn't quite look right, does it, with an A, I believe.
Let's run with that and I'll copy that.
I'm going to head over to exit again, change the layer to be interactive tiles.
Now I also need to click back on this one more time, add a layer and here we find the sorting layers,
click on plus and I will pop in.
Interactive tools will have that just below.
The player I think is fine.
Will the enemies run in front or behind?
Probably the enemies in front because that's what you need to know about just in case we can play around
with that.
But that seems okay for now.
So interactive tools there.
Once again, click back on exit and find our sorting layer interactive tools.
This has been a lot of layers and stuff in this section of the course, hasn't it?
But good to practice with these things now.
It's a bit huge.
I think it might be.
So we'll change the scale to 0.25 and 0.25.
That's a little bit small now.
So I guess 0.5 is the happy medium .5.5 and then drag that over to the right hand side and good.
Now we need to add a box collider so we have a way of bumping into it.
Add component box collider to DX will make this a trigger.
It's going to be triggering things.
And then final step find our prefabs, drag our exit down into prefabs.
That should be pretty good.
Now we need some behavior for this.
I'll hop into my scripts folder.
Right click, create a C-sharp script and I'll call this level exit.
And I'll click into the exit prefab and then drag the level exit over to the add component area.
Now the level exit is on our exit prefab.
Good stuff.
Now let's double click to open up level exit.
I'm not going to need start or update on this.
And you know, what I might do might give this to you as a challenge.
But before I give you the challenge, I'm going to teach you one more thing, which is routines.
And routines are another way for us to create a delay in our game.
So it's going to be really useful to make sure when we touch out exit, we can wait for a moment, say
a second before we load the next scene.
And the core concept here to understand is that we start a process now as we start our routine, and
we do that in the same sort of way that we would call a method, and then we go off and do other things
we yield until our condition is met.
So until, for example, our one or 2 seconds is met.
So that's the general core concept.
Let me give you the code, which I think will make it a bit clearer.
So here's an example or the framework for code, routine code.
We need to call our code routine at some point the same way we call a method, but with some different
wording in here.
So we need to use the word start routine and then we put in parentheses the name of the method.
So if we weren't using a code routine and we didn't use a delay, we might just create a method, and
then the method might just be load the next level and that'd be fine.
We just load the next level.
But if we want to delay, we need to use stock code routine and then load the next level.
And so that's the structure of calling it in terms of the actual construction of our method or our routine,
which is what we're creating when you need to use the keyword I enumerator and then the name of the
method so we won't have a return type, we're not going to have void, we're not going to have bool
or anything like that in there.
We just use the word I enumerator and that enumerates three things.
It keeps a list of things you don't need to worry about the fundamentals of exactly what's happening
under the hood at this stage.
This is just a basic implementation of a code routine, but we need the word enumerator and then the
name of the method.
So for example, load the next scene or whatever you want to call it.
And then in our curly braces, we start off with these very important keywords yield, return, new,
and then we have some options for what we're waiting on or how long we wait or what it is, what the
condition is that's met.
You can create other methods in here that have other conditions in it, but we're going to use one of
the basics, which is wait 4 seconds or I'm going to use wait 4 seconds, real time.
You can use wait 4 seconds.
There's not going to make much of a difference for us, but wait 4 seconds real time and then in parentheses,
the amount of time and you want a parameter rise, the time is put in there.
Say 1/2, that'd be fine.
But you want a variable so you can tune the amount of time.
So this whole line in here, I know it's a little bit crazy, some new words in there, but this whole
line is just basically saying, wait for some stuff to happen.
And when that stuff has happened, then continue on with anything else you want to do after you've waited.
So this line is important.
You can have stuff before this line.
So I could do a couple of things.
So as soon as I call the method, I could play a sound effect.
I could trigger some particle effects, then I could say yield, which means go off and do your other
things you need to do and then return when this condition is met.
Return when you've waited for a certain amount of time and then do the other stuff, which would be
loading the next scene.
So there we go.
It's a slightly different way of introducing a concept where I'm introducing the theory, introducing
the code, and then I'm going to give this to you as a challenge to incorporate this.
The challenge is to load the next level.
When the player touches the exit load the next level, there's a few steps you need to do in this.
We've done this in other sections of the course, so feel free to go check those if you're not exactly
sure.
And for bonus points, wait for a second before loading the next level.
So there you go.
There's your bonus.
If you're feeling a little bit more advanced, there's your challenge.
Take that on C back in when you're done.
Now, any time we're doing loading of stuff, I often forget this.
I'm going to do this.
First of all, I'm going to jump over to file build settings and then I need to go to my scenes, my
levels drag Oh, these will highlight all these 1 to 3, drag them over into scenes in build 1 to 3.
They're in the wrong order.
So I'm going to reorder them, drag one to the top if I can, and then two and then three.
One, two, three.
And that's going to represent zero one and two as our scene index.
Okay.
We've got that.
Now back over into our script.
First of all, we need an on trigger into to DH get rid of the private don't need it tidy things up
a little bit here so I'll do this the simple way first then I'll add something to give us a little bit
of a delay.
So within here I'm going to first of all find what our scene is.
So I need to have some sort of variable I can use var for this when I don't know exactly which variable.
So var current scene index.
It's going to equal what?
Scene Manager Well, we can't see that at the moment because we're not using that namespace.
So let's add using unity engine dot scene management semicolon.
Now when we jump back over here into scene equals scene, we should have some options.
Scene Manager That's what we want.
Scene Manager Dot We need to be figuring out what the active scene at the moment.
So we'll find a method which is get active scene, that's the one in parentheses.
And then in particular we're looking for the build index.
So the build index we're looking at before you've probably done this before, so you know what I'm talking
about, but this is the build index zero one or two at the moment, and that is going to be accessed
by build index at the end and then semicolon.
So that will be stored in this current scene index, which is some sort of variable which if we mouse
over this says that it's an int.
So in current scene index we could call this an int because we know it's an integer, it's going to
be a01 or two.
So at the time when I created it, I wasn't thinking, I didn't want to have to try to figure out is
this an int or a float or something else?
So I just added var and unity figured it out for us, but I can now add int and everyone's happy.
And then when we hit this trigger we want to be loading the next one.
So basically it's whatever our index is now plus one.
So we will tap into our scene manager class once again and we need to load scene.
So that's the method we're calling.
What scene do we want to load?
In our parentheses?
We're going to use our current scene index plus one and semicolon.
Let's save that.
See if this is going to play nicely for us.
I need you anymore.
Click on play.
Run, run, run, run, run, run.
And then think it's going to load the next level, which is pretty cool.
You could leave it at that.
You could have instant loading.
Not a problem.
What I'd like to do is to add a little bit of a delay in here, and the way I'll do that is using a
code routine.
So when we hit our trigger, instead of just saying, do this stuff, we're going to start our code
routine at this point.
So a little bit of space start cur routine and we're going to give it a name.
The name I will give it will be load next level seems to make sense and then parentheses at the end
semicolon.
Now load next level doesn't exist.
So I'm going to highlight all of my code cut that because I need to paste it in, tidy up my formatting
a little bit, jump down below and code routines need I enumerators for them to work so we jump into
our I enumerator and this will be called load next level.
Side note I probably shouldn't have cut all my code and then have it in my copy buffer for quite so
long.
You only say coffee buffer for a second there.
Some of us need that in the morning.
Just a side note, I'm feeling a little bit foolish.
I'm actually just going to paste it down here now so I don't accidentally lose it.
That was a little bit crazy living on the edge what I was doing there.
Now when we create a code routine, the first thing we need to do is be yielding.
So yield, return, new.
That's our way of saying, okay, we want to just hang out and chill for a second here and I'll use
the terminology.
Let's say we've got wait for end of frame.
No, wait for fixed update.
No, wait 4 seconds.
We could do wait 4 seconds or wait 4 seconds real time, which I think is going to be more of an accurate
actually, if I say it's 1/2, it's going to be 1/2 in row, and then how long?
How many RL seconds do we want to wait?
Well, I'll parameterized this as a variable, so we'll call it level load delay.
And semicolon we'll go create that up at the top pump serialize field.
It will be a float and called level load delay and we'll make that one as a starting point.
Now, after we've done our weighting, the bit that I had in my coffee buffer, I can now paste.
Oh, there we go.
I've also pasted it down below, which I'll get rid of in a second.
That was my back up.
That was my just in case.
That was a cup of tea in case there's no coffee available.
Or delete that.
Delete that.
And just tidy out the formatting.
So now we're saying yield for a certain amount of seconds.
We're saying one and then do these things, load our thingy and then we're all good.
So let's see if that works.
Save that up.
Our goal here is to touch the exit sign and have 1/2 elapse before we load into the next scene and run.
Jump one.
No.
There we go.
Perfect.
So the last step in here is for me to go and put my my level exit and all of my levels.
So we'll just do a bit of saving.
And level one, jump into level two.
Where are you going to be?
Down the bottom here.
Did I prefab that?
Did I prefab my level?
And did I put all the stuff on it properly?
I don't know.
Don't pick up my habits at home, boys and girls.
Let me just go back to my level, level, level one, find the exit.
That's the instance in there.
Overrides.
There's no override.
Okay, good.
So everything was on the prefab.
I did it.
Okay.
It didn't make any mistakes.
Phew.
Okay.
Prefabs drag that into our world and then.
Jump into the next level, level three and.
Drag the exit in there once again.
Right in that spot.
It's a little bit big for there.
So in this particular level, we can make the instance of the exit a little bit smaller, make it 0.3
and 0.3.
That fits.
Just because.
Or I could have dug out a little bit more space in there.
Either way, it's all fine.
It's just sort of finessing at this stage.
It's a little bit more sneakily hidden in there, which is, okay, so let's grab our player and pop
our player right next to this exit and see what happens when we touch that one.
Because we know what happens when we go from 0 to 1, 1 to 2.
We're currently in build index two.
What happens when we touch that 1/2 and it says, Oh, I don't know where I should go to just here,
it's going to give us an error or it's just not going to connect.
It's giving us an error down here now saying, Oh, there is no next level.
We're at the highest one we've found, so it's going to skip over that and not worry about it.
But then I figured, Oh, that's kind of lazy.
Let's put in a little bit of code to protect ourselves against that.
Let's rearrange things here a little bit and say to ourselves, integer, let's say next scene index.
So not currency in index, but next scene index is going to equal what we've got down here, a currency
and an index plus one.
So copy, pop that in there semicolon.
So we need a way of comparing what is the next scene index with what how many scenes do we feel is in
our level?
So if the next one is three and there's a grand total of three, we're fine.
If the next one is four, but we've only got three, then we've got an issue.
So if next scene index, in other words, the level we're on at the moment plus one is equal, equal
the same as scene manager.
Once again, we're accessing our manager class within here.
There's this cool property which has seen count in build settings.
In other words, how many scenes are there?
So we're comparing these to if the next one is the same as the total, then first of all, what we need
to do in our curlies here is to assign or reset our next scene index to be zero.
So we're saying, okay, if you've reached the end, then the next one is actually zero, reset it back
to zero.
And from there we can say see manager load scene and just straight up next scene index.
Okay, hopefully that makes sense.
I'll go through it just one more time.
Really quickly in here we need the concept of saying what's coming up next?
What's the next scene we're going to load?
On the next level we're going to load and that's our current plus one.
So from build index zero, the next one is going to be one.
If we're on build index one, the next one is going to be two and so on.
And if we get to the point where the next scene index so in other words, we're currently on two and
we add one to it, that would make it three.
If that is the same as the total number of scenes that we have in our game and we have three, then
we need to reset our next scene, index back to zero.
And then what we want to do is load whatever the next scene index is, which in this case is going to
be zero.
Let's save and see if that works.
The goal here is so that when we're on our very last level, we'll load back to the start.
No matter whether you have two or three or ten levels, this should still work for you.
1/2 delay and back to the start.
Awesome.
And we'll just check that this first one works as well so that we go.
All done.
I hope that's working for you nicely.
I will see you in the next lecture.