[SOUND] In this video, we're giving the player a score,
and at the moment it's zero.
And we're also creating a Points Per Block Destroyed so
that as we destroy the blocks,
you can see over here our current score goes up as those blocks get destroyed.
Let's jump in and create some score.
Let's start by talking about the logic of how we're gonna lay out our score here,
and I've got my pen to do some scribbling.
First of all, I think we'll use our GameStatus script to keep track
of a couple of things, to keep track in our script here, of the current score.
We need to know what is our score right at the moment.
We also need to know how many points to add.
We could just leave it at one, but
I think we'll be a little bit more creative than that.
We'll say how many points to add when a block is destroyed.
And again, I think GameStatus is a good place to keep those things together,
to keep your current score and how much the score could go up in that same place.
And then we need a place to say, well, when something happens,
then we need the score to go up.
And I think the logical place to put that is when our blocks are broken or
destroyed.
And the blocks are destroyed, and this would be on our block script down.
So I used the fatter pen for this drawing.
For some reason I'm kinda drawing on an angle, but
the block script that I'm hung on is going the top of is where we'll keep track of
the fact that it has now been destroy and you should add more points.
So it's saying, yeah, add the points now is where this will be called.
So to summarize, we need a place to list out what's our current score,
need a place to say how many points do we get each time something is destroyed.
And we need a place to say, hey, I got destroyed,
please add p0oints to the score.
So let's jump in and get cracking on that.
Let me just turn my pen off and delete all this stuff off the screen.
I'm gonna open up Visual Studio and go over to game status.
The two scripts that we're gonna be working on in this lecture will
be GameStatus and Block.cs.
So first of all, in GameStatus we want to do a couple of things.
I'm just gonna put in the headers here to make it clear where we're grouping
our variables.
This is our configuration parameters at the top.
And then we're also gonna have some state variables that are keeping
track of what's going on at this point in time, what's our current state.
So within our state, we need a way to say, what's our score at the moment?
I'm gonna serialize this so that we can see it in the inspector,
as we've done in the past.
I don't really want to be changing this as a designer,
I just wanna be able to see it very easily.
Until we're printing it on the screen as UI, for now we just wanna be able to
see it in the console, so SerializeField is gonna be an integer.
We could just call it score, that's a fairly standard convention.
But I'm gonna be a little bit pedantic with this and
call it currentScore so I'm really, really clear what it means.
And we'll initialize it at 0, just so we know for
sure the player is starting off at 0.
And if the game resets, so if we restart the game,
then we're again initializing our score back to 0.
Okay, and then we also need up in our configuration parameters something that we
can tune, which will be a Serializedfield.
Again, this will be an integer, and I'll call this,
this is gonna be how many points you get when you destroy a block.
So again, I'm gonna be really detailed and call this ScorePerBlockDestroyed.
Now, that's long and it may be a little bit more than you need.
But I don't wanna be looking back on this in a week's time and
have something that says score points or something and not know what that means.
Actually, I'm gonna go one step further and say points cuz it's not score to add,
your score is your total.
It's gonna be pointsPerBlockDestroyed.
Okay, and we'll initialize that at, all right, we'll say, we'll make it 83 points.
Right, now we've got our score, we've got our current score, the points per blocks.
Now we need to create ourselves a method that says to add up our score,
to add something to the score.
I'm just gonna get rid of my start method, don't need that at the moment.
And we will, underneath update, it's good to have update first, so
we can see what is going on every frame down underneath update.
I'm going to create a new method.
It will be a public method cuz we want to be able to call this from a different
script.
We wanna be able to call this from our Block script, so
therefore we need to make it public.
Public void, what are we gonna call this, my friends?
Let us call this AddToScore.
So we're gonna add something to the score.
We're gonna add up the score whenever we call this.
And in here, our code needs to be our current score.
So what is our current score?
And then I'll show you the long code and
then I'll do a slight refract to make it be shorter.
Our currentScore =, what do you think this is going to be?
If you wanna do a little mini challenge, pause the video.
When we add our score, it's gonna be currentScore = what?
It's gonna be currentScore = currentScore + pointsPerBlockDestroyed.
Okay, so another way that we can write this is
currentScore += pointsPerBlockDestroyed.
So that's a shorter way of writing just the same thing that we were writing
before, currentScore += pointsPerBlockDestroyed.
Let's save that, and just to make sure that we've got those variables that
are now being serialized, so just move my screen a little bit.
Okay, so points per block destroyed is 83, great.
The name might be a little bit too long cuz it's having trouble fitting in
the Inspector, but we'll keep it as is for the moment.
And then the current score is zero, great.
Now, what we need to do is each time we destroy a block,
we need to increase our score.
That's gonna be your challenge.
So add the score when block is destroyed, in our Block.cs call AddToScore,
it's as simple as that.
So that's your challenge, I'm gonna give you a hint in a moment.
If you're ready to dive into that challenge, then jump in now, take that on.
This is a good one to think about, you've got all the clues in previous lectures.
So I'd really urge you to take this on as a challenge now,
particularly if you've just been cruising on the challenges.
If you give it a shot and you're not sure, then you can come back and
have a look at these hints, and I'll give you those hints now.
Hint number one is add the score when the block is about to be destroyed.
That's hint number one.
And hint number two is use FindObjectOfType somewhere
when you are writing your statement to call AddToScore.
Okay, so there's your challenge, there are the hints.
Jump in and take that on now, and I'll see you back in a moment.
Okay, so we need to head on over to our Block.cs, we'll have a look in here,
where is the block being destroyed?
Well, it's being destroyed OnCollissionEnter,
we've already got a method in here we're calling that's doing all the destroying of
block-related things.
So I'm gonna add this into our DestroyBlock method.
It's getting a little bit full and it's doing a few different things, but for now,
I'm comfortable in our DestroyBlock output in our adding to score.
We'll do that right at the top, even before we have any sound play,
we'll say findObjectOfType.
And then what's the object we're looking for?
Well, it's our class, or our script, GameStatus.
GameStatus, and then we put in our parentheses cuz we're not feeding in,
passing in anything, dot, and you can see it's right at the top here,
AddToScore, and then ();.
So what that is doing when the block is destroyed, when we're saying, hey, destroy
the block now, we're also saying, go and run this method, which is AddToScore.
Hop over into Unity, I'm going to add a few more blocks into my scene so
that I can have a few more things to hit to see what score got.
So just duplicate and then hitting the W key, moving it over to the left,
duplicate.
Duplicate and another couple of times duplicate, and one more duplicate.
Okay, there we go.
Now, what I want you to look at, make sure we clicked on GameStatus, click on Play.
And then we're watching our Current Score over here while also playing,
which is no mean feat.
It's going up 166, 249.
Okay, do I like that weird, random score amount?
Yeah, it's fine for the moment.
Okay, 333, great!
So we've successfully implemented, I'll click on Game Status,
our current score going up by points that we can change, so
that we can give the player some sort of feeling of progression and score.
Great work, well done, and I'll see you in the next video.