[MUSIC]
In this video, we'll start the process of allowing the player to win on
a level by adding up the number of blocks that are now seen, so
we know how many blocks are remaining.
And if it gets to zero, the player has won.
So, let's jump in and start the process of giving a player a chance to win.
Let's look at our flow here.
Here's Our Workflow To Win A Level.
I'll break this over the next two lectures.
So I'll show you the process for two lectures and
then we'll just do half of it now.
We need to create a level GameObject and
a script so we have a way of keeping track of where we are in the level.
We need a variable to keep track specifically of
how many breakable blocks there are at the start and
how many breakable blocks there currently are in the game.
We need a method to add up all of the blocks.
We need a method to signal that a block was broken.
And we need an if statement to load the next level if the blocks reach 0.
And then to tell each block to add itself to the total at start, and
then to tell each block to remove itself when destroyed.
So that might seem like a big old list of, I don't know how to do it.
If that's the case, no problems, we're about to go through it together.
And if you're new to programming,
you shouldn't necessarily know exactly how to do this just yet.
But if you wanna take on a big challenge, a big whopper of a challenge,
have a look at this list here should be all you need to create the process where
destroying the last block in a scene loads the next scene.
So there's a bit of a mega challenge for you if you're ready to take that on.
If not no problem.
So what we need to do, first of all,
is create ourselves an empty game object, we'll call this Level.
And as we do, we'll reset, so right-click reset transform.
Reset transform, I should say.
And this very early stage, I am going to create this as a Prefab.
Drag down into our Prefabs folder So it's a Prefab straight away.
So any changes we do,
we need to remember to click apply to apply it back to the prefab.
This is something important that will allow us to use this level prefab
in multiple levels.
Okay, next up we need to add a component and this will be a script called level.
So level, new script, create and add.
Excellent, let's open up that Level script.
Double-click on that and while it's loading up,
I will be a good individual and find where we just created that.
It's in our Assets folder.
Drag it into Scripts, so it's sitting in there nice and neatly.
And I don't think it did load.
So we'll triple-click on it, that should load that time.
There we go, just needed that extra clicks, three clicks is a winner.
Okay, so within level what do we need to do?
First of all,
we need to give ourselves a way of keeping track of how many blocks are remaining.
So as I int breakable, breakable blocks as a variable.
And you know what I'm gonna do, is I will serialize this.
So, serialize field, so that we can see it in the inspector so
we know how many are remaining.
This allows to do some debugging.
Let's make a note here.
Serialized for debugging purposes.
Just in case we wondering, serialize, serialized, serialized,
I think that's how we spell it.
Just in case we we're wondering in the further why on earth do we make that
available because we don't want ourselves or a designer to come in and say you
know what instead of 50 breakableBlocks at that start, I think maybe there's 80.
So, we're not doing this so we can change it,
we're just doing it so that we can see it.
Okay, save that and continue on next, you know, what I'll just remove this and
start an update methods.
We're not gonna need those at the moment,
I like to remove them until we need them to keep everything nice and clean.
What we'll need to do is make a public method, so public void.
What should we call this?
We'll call this count breakable blocks,
which sounds like a Sesame Street character, but it's not.
Count breakable blocks, no semicolon Rick and open and close parenthesis.
And open and close curly brackets.
What do we need to do in here?
Well, basically what we're saying is each time this method is called,
we want to add one more breakable block to the total number of breakable blocks.
So we'll say breakableBlocks, what do you think we do next?
We say plus, plus.
That just means take whatever your value is now and add one more to yourself.
Okay, so we should now have a way of saying yes, we do.
How many breakable blocks are there in our scene and
a way of increasing the number of breakable blocks.
Now, I'm gonna show you something new that we haven't gone through before.
If we go over two block over here.
We need a way for us to access our public method that we've just created and
we've done this before but we've done it before in more of direct way of creating
a field in the inspector and then pointing something to that.
So, for example we could this is an example.
I'm going to delete this in an moment, so no need to follow along.
We can [INAUDIBLE] serialized field.
Something that is of type level.
And we'll call it level as a variable.
So we need something type level that is called level.
If we were to save this.
Jump over to our inspector now while clicking on, what were we in just before?
We were on block.
While clicking on block.
We can find one of our blocks, and we can see down here there's a level, and
we go and drag our level, [SOUND], and drag that over into here.
So that's the linking up way of doing things.
Totally cool, gives you a lot of control, and it certainly is idiot proof because
If we, the only way that we can access level and
call the methods that we've just created on level,
we've created them in our level script which is on our level game object the only
way to access those is if we've gone and dragged our level over into here.
So that's one way of doing it.
I'm going to remove this because that's not how we'll do it,
we'll do a new thing, which is gonna be cool, which is find object of type.
So we still need to have a variable so we can cached our reference, and
we have done that in previous videos.
So level, level.
It's a of type level and this will be our,
I'll just make a note in here, cached reference that we are caching.
We need to put back in our start method.
So if you type in Start and then Tab or Enter,
that should autocomplete the rest of it.
And we need to find the object of type.
We haven't seen that before, so let me type this in, FindObjectOfType, open and
close the angle bracket.
And what do you think the type is that we're looking for?
Well, the type is level.
As straightforward as that.
Now we're saying find an object of type level but what do we want to do with this?
Now, let's put my open and close brackets in here while I remember.
Well, we need to assign Level to be that, so
we'll say level equals FindObjectOfType and then semicolon.
So just to repeat that we're looking for a particular type of thing that is level,
that's our class.
And our class level has all of our public methods that we're creating that we
want our blocked to be able to call,
for our blocked to be able to call it we need access to level as a class.
We're doing that by saying find object of type level we're assigning that to our
variable of level and
then our variable level where caching is a reference up here.
So from this point on it knows whenever we say level throughout our block script,
it knows that it's talking about finding this object of type, okay.
Hopefully that's nice and super clear.
And then the last step in this process is the challenge that I have for you which is
to update our Block.cs to call the method for counting blocks in the Start method.
Remember we need to find the Level class using FindObjectOfType,
which we have just done.
So we now have access to Level, but we need to include Level in what we do.
Okay, so there's your challenge.
Jump in and see if you can increment the number of blocks we have in our scene and
then test that it works.
See you in a moment.
Right, so we just need the one line in here pretty straightforward.
We're saying level dot and then what is the name of our method?
Well it was CountBreakableBlocks.
Open and close parenthesis, we're not passing anything in.
Semicolon.
So now from within block.
It is saying, call this method
that method is saying add one to the total number of blocks.
The total number of blocks is stored in breakableBlocks.
So, let's jump over into our scene.
Now, we saved those, jump into our scene and
nothing will happen until we click play.
But I'm just gonna click on Level here.
You know what I'll do, is I don't like Level sitting down outside.
I'm just gonna drag Level up to near the top, pulling underneath the Play Space.
Just mentally for me that makes more sense.
Rather having it sitting all the way down the bottom.
So Level currently it says Breakable Blocks zero.
While we have to click Play to call our start method and
then you see breakable blocks ten.
One, two, three, four, five, six, seven, eight, nine, ten.
Excellent.
Fantastic.
So we now have the total number of breakable blocks being counted up at
the start, which is going to give us a mechanism to say when this reaches zero,
we need to load our next level.
And that's what we'll do in the next video.
So, great work and see you again soon.