[MUSIC]
In this video we're going to hook it up so that when all the blocks are destroyed,
we load our next scene.
Super exciting, so let's get started.
One thing I didn't talk about fully in the previous video that I'll touch on now is
each of these blocks, you've got one, two, three, all the way down to ten blocks.
Each of these blocks contains the script block.
And on that script block we have, we're saying, we're calling the method,
CountBreakableBlock which basically just increments the number of blocks by one.
And the reason that adds up the total number of blocks is when we start
the game, every single one of these instances, these are instances of our
prefab, every instance says, do the thing that is on my script.
So count me as a block.
And then this one says, add another block.
And this one says, add another block, add another block.
So each of these are saying add another block.
And so they're doing that ten times.
That's why it's adding up in our level.
It's adding up all the breakable blocks.
Each of them have said count me, and that adds up to ten.
Okay, so just wanted to be super clear with what's going on there.
Now we're gonna jump into level.cs.
Again, we need to create ourselves our next public method.
Cuz if we look at our flow we can see that we have gone through,
we've created a level object, we're keeping track of breakable blocks,
we're adding up the blocks.
Now we need a method to signal that a block was broken,
an if statement to load the next level if the blocks is zero, and
then also to tell each block to remove itself when it's destroyed.
Remove itself from the total number of breakable blocks, and
that's what we're up to at the moment.
The first thing we'll do is create a public method, public void, and
we'll call this one block destroyed.
Block destroyed.
I have an enclosed parenthesis, I have enclosed curly braces brackets,
and then within here we want to say,
well I'm gonna basically say the opposite to what's happening up in here.
When a block is destroyed we want it to say hey, decrease the number
of breakable blocks by saying breakable blocks minus minus, and then semicolon.
Now what else do we need to do?
Well, within here as well, at the same time that a block is destroyed,
we want to say right now hey, have the number of blocks reached zero?
So put in an if statement, if, and then our parenthesis we
wanna say if the (breakableBlocks) is less than or
equal to 0, then what do we want to have happen?
Well, what we wanna have happen is to load the next level.
So we know from our ScreenLoader, so we have a ScreenLoader class.
We've already created a load next level method, so we wanna call that.
If you remember, I'll find object of thought.
And once again, I will cache this as a reference just so
that we don't have to find the object of type every single time.
And so to do that we will say SceneLoader,
capital S, SceneLoader, and then sceneloader.
So this is just a way of saying we now have a variable,
sceneloader variable that have type SceneLoader.
And again I'll just make a note in here, cached reference, so
that we know this is our cache not the thought we have,
our parameters, parameters, that we need to keep track of.
Just helping you guys see that this is the structure we have at the top of our
script, so you can get into the habit of putting things in the right place.
You don't have to write in your comments each time,
I'm just doing that to make it clear from a teaching point of view.
Okay, so we have our scene loader, now if breakableBlocks is less than,
sorry in our start method now.
I've jumped a step ahead.
What do we need to do next?
This is a question for you.
Many challenge for you.
How do we get access to our sceneloader?
How do we cache that?
Well, we say Start.
Hit Tab to autocomplete, and then we will say sceneloader = what?
Another mini challenge for you, complete that sentence.
sceneloader = FindObjectOfType.
Angle brackets, and within here we're finding capital s breaks Sceneloader.
Open and close parenthesis, semicolon.
So now we know that SceneLoader is a cash reference to the class or
the objective type SceneLoader.
Now in our if statement down here, we can say, cuz we need to access sceneloader.
What are we accessing in there?
We're accessing load next scene.
So, here's another mini challenge for you.
Let's load the next level.
Okay, you're just waiting for me to continue,
are you actually doing these mini challenges?
It's okay if you're not but this is how you really get better at these things.
If you push yourself and pause and try to figure it out on your own.
That's how this really embeds.
It's cool if you're not, but it's even better if you are.
So we'll say in here sceneloader.LoadNextScene.
Open and closed brackets and semi-colon.
So now we've created our, when a block is destroyed, we want to yell hey,
block is destroyed.
We decrease the number of blocks by one that are breakable, and
then we say if it's reached 0, then load the next scene.
I'm gonna save that hop over into block, and
this is where I'll give you your proper full on challenge.
That was our last level.
It's our last lecturer.
That's what I'm after.
Update Block.cs.
So here is the challenge.
Call our method for decreasing the number of blocks when a block is destroyed.
Update Block.cs, and ensure that you have your build settings correct.
We haven't done that yet for this scene.
You might have done it but I haven't done it yet.
And to test and rejoice that it works.
And another little hint for
you, you might wanna decrease the number of blocks in your scene to one just so you
don't have to get rid of all of the blocks you can text it a little bit more quickly.
Okay, so as your challenge jump in and get started.
Okay, so within Block.cs, we need to find our OnCollisionEnter,
this is where things are getting destroyed.
Underneath Destroy, we want to give an update in here that says,
well not an update, but we wanna call our method in here.
I'm thinking that it's updating the situation but it's not the update method.
We're saying level, we've already created ourselves a cash reference to the level.
So level., and what do we have in here?
It's gonna be BlockDestroyed );.
So we're saying access the level class that we're doing and lock is destroyed.
Pretty straightforward.
Now I'm gonna do one more step while I'm in here which is just to factor this out
to clean it up.
This is a refactor, it's not critical.
Things will still work without it, but this is just for readability.
Not to do too many things under this OnCollisionEnter.
So I'm gonna highlight my three lines here, right-click.
Slightly different on the Mac, you'll see.
Quick Fix and then Extract Method.
On a PC, Quick Actions and Refactorings, and then Extract Method.
We'll call this method DestroyBlock, and then hit Enter.
So basically, Whenever we collide with the block, then we want to destroy the block.
Okay, I'll call DestroyBlock or, yeah, that's good for now.
It'll change a little bit later on, but for now, this is what we've got, so
this is the best thing to call it.
Save that, jump over into our game.
Now, I mentioned about Build Settings, Edit.
Sorry, File > Build Settings.
And one thing you'll notice with our Build Settings is that we don't have any level
two sitting there.
So I'll just click Add Open Scenes, cuz level two is open at the moment.
Now I'm gonna cheat a little bit and drag this above level one so that when we win
level two it'll take us to level one, just to make it easy for testing purposes.
And as I mentioned before I'm gonna select all of my blocks except for one.
Delete those, click Play, we should see if I click on level,
we should see that there are one breakable blocks, yes there are,
click back in my game window, [SOUND] and then plonk, and Game Over.
Didn't work, okay, what have we not done in here?
We're looking for in our level, we didn't go through this quite in enough detail.
We have SceneLoader, I've forgotten that we don't have SceneLoader in our scene at
the moment, do we?
That was an assumption that I made was incorrect, so on level I'm going to drag
in SceneLoader, so drag SceneLoader as a component on level now.
When we're saying fine object of type, here it is right here sceneloaded.cs
is another way of saying it's the SceneLoader class,
and it's in our scene because we have it sitting right here on level.
Make sure on level we can click Apply, so it applies all these back to the prefab,
so when we drop in level and future levels, it knows what's going on.
So just a reminder, why I was doing that is we're looking for
FindObjectOfType SceneLoader, but
we didn't actually have any SceneLoaders in our scene just yet, and now we do.
So save everything up, click on Play, look to see if there's any errors.
I don't know how long that errors been there but it's now gone.
So there is no error, fantastic.
And we click and it should, plonk, load into the other scene.
That's great, very excited to see that.
So excellent work, we now have a fully functioning game.
You can add a couple of these blocks back if you want.
I'm just gonna add one more, so we've got two sets.
It looks a little bit more like a game.
And so when we complete this scene, it will load the next scene.
That area has reappeared down the bottom of the screen but
that should go away when we click Play.
We can now play fully through a full game loop where the player can start,
win, lose, and restart the game.
So great work, and I'll see you in the next lecture.