We will create a script in order to keep track of how much this is.
A player game object made our score.
So how much score we made so far?
So we're gonna keep track of that.
So we're going to create a script for it.
So let's move over to this project tab, then assets and then script folder.
So let's right click there create and then Monobehaviour script.
And you can name that script to something like Score Manager.
I already have created a script called Score Manager.
And over here you can see that.
So what we need to do now, we need to attach this script to any game object of our hierarchy.
Or we can simply create a separate GameObject for this script.
But I'm going to attach this script to my player game object.
So I will select my player game object in the hierarchy, and let's select script and drag it over to
this Add Component section in the Inspector.
And we're going to double click this in order to open it up in Visual Studio.
And we will go to the top of our class.
And what we're going to do, we're going to make a serialized film, serialized film that will be private.
So even we set it to private, we will still gonna be able to see this parameter underneath our score
manager script.
Why?
Because we just marked as a serialized field.
As you can see.
And that will be float.
And we can simply call this one to something like current score.
So let's call it current score, current score, current score.
And let's close that up with semicolon.
And what I'm going to do I'm going to move over to this start function.
So this start function get call in this start of the game.
So in this start of the game I just want to set the current score to zero.
So I will set current score equal to zero f.
And let's close that up with semicolon.
So why we set it to zero.
Because in the start of the game we're gonna have zero score.
So that's why in the start function I just set a current score equal to zero.
And now what we're going to do we're going to move over to this update function.
So we do know that update function runs every single frame.
So every single frame, what I want to do, I just want to keep increasing this current score.
So top of this current score, I'm going to add some a number in order to increase that.
So we will do current score and top of that plus equal we're going to multiply something like 0.1 or
0.1 f.
So I'm going to multiply with 0.1 f or basically simply a lower number.
And make sure you just multiply this number with time.deltatime.
So over in frame rate this number just going to be constant means every single time this number will
be constant.
And if we just don't multiply this number with time.deltatime then this number will gradually increase.
So to just make it a constant we just multiply it with time delta time as you can see.
So the current score in the start of the game is zero.
So zero.
And top of that we just adding every single frame a we're doing inside this update function.
And we do know that update function runs every single frame.
So every single frame we just add on top of this current score some small number.
As you can see.
And also make sure you just multiply with time times delta time to in order to make that number constant.
Now we're going to press down Ctrl S to save our skin.
And let's move over to unity.
So as soon as we're gonna start our game, uh, you will see the current current score just gonna increase,
uh, over again and again.
So just wait for to compile, and we can see underneath our score manager script.
Current score by default is zero.
And if we just click on this play button in the top, you will see as the time progress this current
score just gonna keep increasing over and over because we're just doing inside that update function.
And as you can see as soon as the game started we can see our current score just increasing.
But the problem is that we can see this is too much even it's not touching at 0.1.
Sorry one.
And now it starts.
One.
So what we will do, we will get out of play mode and we will move over to script.
So we're going to increase this number instead of passing point one we're going to pass something around
ten to just increase uh, a uh to just increase our current score a little bit fast.
So what you can simply do, you can make a variable for this as well by going over to this, uh, serialized
field.
So let's make that.
So we're going to make a another serialized field.
So make sure you just make this square brackets first.
So I'm going to make square brackets sorry.
And we're going to pass serialized field.
And we're going to call this one.
So this will be private float.
And we will call amount.
So let's call it amount something like that.
And by default I'm going to give a default value.
So set I'm going to set equal to ten.
And now I'm going to replace this ten f with our amount.
And we can adjust it underneath our score manager script.
So that's why we just marked as a serialized field.
So it's just going to make a private field a public.
So we will see this parameter underneath our score manager script, which will be ten by default.
We set but we can adjust underneath our inspector.
So that's why we just marked as a serialized field.
So we don't need to come to this script and change this number all over again and again.
So that's why we just created a created a variable for this.
And now we're going to press down Ctrl S to save our script.
And let's move to unity by now.
And let's have a look.
So underneath our current score we will see.
We will see another field just going to appear amount.
And by default we set ten unit.
But you can just give it any number that you like I'm gonna live with ten.
And let's click on this play button in the top by now.
And let's have a look on this.
So the current score by now it's just going to keep increasing a little bit fast.
The current score.
Let's have a look on this because the amount is big.
And we can see the current score is now increasing too much fast.
It's already touched over 50 cross over 50, 70 and 80 and the list just goes on.
But the problem is that if we just die.
So let's take damage from this enemy game object.
And indeed we just taking damages from this and we can see our player game objects now get destroyed.
But the problem is that we have done a mistake.
Basically, if we select our player game object in the hierarchy and we do know that we just attach
this score manager script to this player game object.
So once we destroy our player game object, this score manager is also destroying with it.
Why?
Because this score manager script is a component of this player game object.
So instead of attaching this score manager game object to this player score manager sorry score manager
script to the player, we need to attach A in somewhere else.
So let's create an another game object for this script, because this score manager script also destroying.
And we don't want that to be happen because we need the score.
Sorry.
We need the score manager script in other things.
So that's why we're going to right click on this Score Manager script.
And let's click on this remove component.
And we're going to attach the Score Manager script to any other else game object.
Or we can just simply create a another game object for that score manager script.
So let's go to hierarchy.
Let's right click or hit this plus icon.
And let's create an empty game object.
And I'm going to call this game object as the name of my script.
So which is score Manager.
So let's call it score Manager.
And let's go to transform right click and reset.
And I'm going to select my Score Manager game object.
And I'm going to drag it on top of my hierarchy so I can see all the time.
And now I'm going to select Score Manager Script.
And I'm going to drag it over to this Add Component section.
Now it's fine and we hope it will work fine.
So let's click on this play button in the top by now and let's have a look.
So by now this score manager script not gonna get destroyed because we just attach this script to other
game object which we not destroying at all.
So we're gonna have the score.
So just wait for it to compile.
Sorry.
Wait for it to die.
This player game object.
And I really apologize for it, that we attach this score manager script instead of adding to a separate
game object.
We just added to this player game object and we can see, uh, player game object is dead by now.
But the problem is that if we just go over to Inspector underneath our score manager script, we can
see the score current score.
It just keep increasing and increasing even after our player game object dies.
So once our player game object dies, we need to stop increasing.
So we're gonna get out of play mode to stop doing that.
We do know we just created a game manager script, which is just keeping track of the player is active
or not.
And we do know once we destroy our player game object we just created a parameter called is Game Active
and we're setting it to false once our player game object died.
So if you haven't seen the lecture yet, then you can just move over to my previous lecture.
In this course you and I have in that title, which says Gamemanager script in order to control the
game is active or not.
So if you haven't checked that out lecture, then you can go back and check the game manager script.
So we're going to use that logic over here.
So that's why.
So let's open up our score manager script and let's double click that.
And we before we add score.
So we're adding over here.
So before we add score we're going to check here.
So we will use this if condition and make first bracket and some curly brackets.
And what we want to check here.
We just want to check if the player game object is uh is active or not.
Or if the player game object dies, mean destroy.
Uh, in that situation, we need to stop increasing.
So before we keep increasing our score, we're going to check.
So we will check.
So we can use this.
Find any object by type and make this arrows and into arrows.
You need to pass the component name that you're looking for.
So we're going to pass player over here.
And we're going to make some first brackets.
And one more first bracket.
And we will check if it's equals equals to null.
And null means empty.
So what we basically doing.
We're just going over to player skip.
So you need to use this.
Find any object by type and the object by type you're looking for.
So you need to put in this arrows.
So the script we're looking for is a player.
So basically we're just going over to this player skip.
And the player script is attached to this player game object we can see in the inspector.
So we're checking if this player script is equals.
Equals to null means we don't have any player skip in hierarchy as well as in this scene.
View means our player skip get destroyed.
And we do know once we destroy this player game object, this player script will also be destroyed.
Because this player skip we can see is attached to this player game object.
So once this player game object destroy this player Skip will also be destroyed.
So what we're doing over here we checking.
As you can see we're checking if player is a script is equals equals to null means we don't have any
player skip in the hierarchy as well as in this interview means player game object dies.
So in that moment what we need to do, we need to stop increasing our score.
So that's why we're going to type this return.
And we do know after return whatever we're going to do it's not going to be execute.
So before we type in return what we check what we're doing, we're just checking if there is no player.
Skip means the player game object dies.
So if that is the case, then we're doing return, as you can see.
And after return, whatever we're going to do, it's not going to be execute means our current is current
score will be stuck.
Uh, we're stuck at where player dies.
So it's going to stuck at that point.
So basically that is the thing.
I hope you get the idea or what we can simply do instead of using this.
We're going to use this game manager instead.
So if you're not familiar with this then you can just use that one.
Or if you just, uh, watch my uh, singleton uh, or clean keep uh, means, uh, clean code, uh,
lecture.
Uh, if you watch that, then you can just use this game manager dot instance, and we're going to access
that this game at a parameter.
And we're going to check if it's equals equals to false.
If that is the case basically means the same thing we're doing means there is no.
And we do know once we destroy our player game object, we just setting this is game active parameter
to false.
And we're checking over here.
As you can see firstly we just moving over to camera gamemanager script, then instant, and then accessing
the schematic parameter, and we're checking if it's equals equals to false.
If that is the case means we don't have the player game object.
Basically, once we're destroying our player game object at that moment we just are setting this is
game parameter to false.
And we're checking over here.
So if you watch the singleton uh pattern lecture or the uh clean code lecture, then you can use or
you can just simply use that which is the find any object by type as well.
So I'm going to press down Ctrl S to save my script.
And let's move over to unity and have a look on this.
So just wait for to compile.
And it's done now.
So if I select my score manager script, uh sorry GameObject in the hierarchy.
And let's click on this play button in the top to play our game.
You will see as soon as this player GameObject just gonna destroy the current score will stop increasing
because we're checking before we adding current score.
So just wait for it to compile.
Sorry.
Wait for it to die.
This player game object.
So let's get closer to this enemy game object in order to take the damage.
And we can see the current score is already now 160, then 70, and it's just rising and rising.
But once our player game object get destroyed, we can see underneath our score manager script.
The current score just stops at 203, as you can see.
So we're going to get out of play mode.
And what we have to do instead of showing our current score underneath our score manager script.
We also need to show show this in our UI so player can know how much score they made so far.
So we can just use that UI in order to show that up.
So we're going to select our canvas in the hierarchy, and let's press down F in order to focus on that.
And let's click over here.
We're gonna move over to this UI game object.
Uh, what we can simply do, we can just right click on it or on this UI game object, then go to UI
and then legacy in the bottom.
And then choose this text.
And you can just name that text whatever you want.
But instead of doing or making it from scratch, I'm going to simply duplicate this current coin at
in order to make the score one.
So I'm going to select that current coin text, and I'm going to press down control D in order to duplicate.
And I want this to be to be moved in the top right section.
So we're going to select that.
But before it we're going to name it to something like uh current score text.
So instead of saying current coin text I'm going to say current score current score text.
So let's call it current score text.
And we also want to move this one to the top right.
So we're going to select that current score text.
Let's inspector.
So Inspector rect transform click over here.
And we're going to hold down alt.
And we're going to choose this top right one.
And now we can see as soon as we chosen that this takes a current coin score takes just gets to the
top right corner.
And I want this takes to be start from a from the right side not to the left side.
So what we're going to do for that we will move over to this text component in Inspector.
And we're going to scroll down and alignment over here I'm going to choose the right one.
So the right section.
So our text will keep increasing from the right side not the left side.
So you can just choose the alignment.
And I'm going to move it somewhere over here a little bit up something like this much.
And we can see this is this seems pretty much fine.
And we can just go over to UI and we're going to move over to overrides and then click on Apply All.
So what we basically need to do we need a reference of this current score cortex.
Then we can just change this up with our score manager, which is our current score.
So basically we just doing the same thing as we have done with current coin takes as well as our health
text.
So we're going to approach the same way.
So let's select the Score Manager GameObject in the hierarchy.
And let's open up the Score Manager script.
And let's go to the top of our class and what we basically need.
We need a reference of that text component.
But before we use that, make sure you just go to the top of your class and include this namespace called
using Unity Engine dot UI.
So whenever you're working with UI, make sure you just go to the top of your class and include this
namespace called using Unity Engine UI.
And now we are ready to grab the reference.
So I'm going to make a another serialized field.
So let's make a serialized field.
And that will private private.
And the component we are looking for is text type.
So we're going to type this text and I'm going to call it something like current coin text current.
Sorry not coin I'm really apologize.
Current score text.
It should be current score text I'm going to call.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save our script.
And let's move over to unity.
So underneath our score manager script we're going to able to see a current score takes a parameter
or a slot.
So what we need to do there we need to drag this current score takes a game object into that slot.
So we're going to have the reference.
So I'm going to select our current score text underneath our canvas game object.
And we're going to select that and drag it over to this slot says Current Score Text.
And we can see by now we have the reference called current score text text reference.
So basically we basically have this current score text or this text component reference.
And now we are ready to link this up with our current score.
So let's open up our Score Manager script and let's move over to this update function.
So update function runs every single frame.
So every single frame we also want to show up or we're gonna change.
But this is not a perfect way uh perfect way.
But it it's fine if it's, uh.
So let's do it inside this, uh, inside over here instead of doing here.
So what we're going to do.
So over here, as you can see, we're checking if the player game object is active or not.
Means if the player game object dies, then what we're doing, we just, uh, typing return, as you
can see.
Or as we just saying return, or we can simply do outside if this if condition as well.
So let's do outside.
So it's going to be much uh easy to understand instead of doing inside this if condition.
So what we're going to do now, we're going to move over to current score text.
And then we're going to access the text.
And we will set the text equal to our current score.
And let's close that up with semicolon.
So the current score As you can see over here is a float parameter and this current score.
Tex dot text.
Right over here is a string parameter.
So what we need to do we need to convert this float parameter, this current score in order into a string.
And in order to convert that we basically have to use these two, uh two string dot two string and then
close that off with semicolon.
And we're going to press down Ctrl S to save our score, uh code.
And let's move to unity.
So basically what we're doing, we're just going over to this current score text game object and then
accessing the text.
And we're setting the text equal to our current score.
So let's suppose current score over here is ten.
So the current score text dot text right over here is just going to be ten right away.
Why.
Because we're directly setting it as you can see.
So we simply moving over to this current score text game object.
And then we accessing this text.
And we're setting this text equal to our current score.
So it's just going to be update with our current score or the score that we made so far.
So let's go to unity by now and have a look on this.
So we're going to click on this play button in the top.
And you will see as soon as the current score just keep increasing.
The current score takes will also just going to increase with our current score.
So as you can see it just screen.
But the problem is that there is a floating number or this decimal number.
So we don't want that.
Instead we just want to be a whole number like 7080, 90.
And the list just goes on instead of decimal places.
So we're going to get out of play mode.
And in order to do that, what we basically have to do, we need to move over to this script.
And over here, when you just converting that float parameter into a string, then you can just put
in this first bracket right over here in quotation.
In quotation you need to pass this zero.
And once you pass in between this quotation zero it's going to convert that decimal numbers into a whole
number, or basically it's just going to show up the whole numbers, not the decimal places.
So make sure you just do that.
Firstly, you need to make this quotation.
And in between this quotation you need to pass zero, uh, the whole number.
So make sure you just do that and uh, use this uh, current score dot.
Uh, basically this dot two string function in order to convert that float parameter into a string.
Now we're going to press down Ctrl S to save our script.
And now let's move over to unity.
So by now we should be able to see only whole numbers not the decimal places at all.
So let's have a look on this and we will see that.
And let's click on this play button in the top by now and have a look.
And you can increase the amount as well.
If you just want to increase your current score too much fast way, then you can just increase the amount
parameter as well.
So now as soon as the game started, we can see the current score just increasing and the score is now
in whole numbers, not in decimal places, and that seems pretty much fine.
So let's take damage from this, uh, enemy game object, and we will see as soon as this player game
object is just gonna lose all of his health.
We can see, uh, this current score or the current score just stops increasing.
And we can see in the UI as well over here underneath our canvas game object.
So basically, that is the pretty cool thing.
I hope you get the idea that how you can do it, or how you can show up the score that you just created
in the UI, so you can just do that in order by creating a separate script.
I hope you get the idea.
So I'm going to get out of play mode and we will go to UI Game Object Overrides in Inspector, and we're
going to click on this apply All and as well we're going to make this Score Manager script This one.
Sorry.
Not.
I'm really apologize for it.
Instead, we this score manager game object and we're going to make this one a prefab.
So we're going to have the copy.
So let's select the Score Manager game object in the hierarchy.
And let's drag it over to this prefab folder in order to make it a prefab.
So I'm going to move uh I'm going to drag it over to this objects uh sub folder.
Uh, this objects uh prefab sub folder.
You can simply say and let's press down Ctrl s to save our script.
And make sure you just attach that score manager script to a other game object, not in specifically
to this player game object, because we're destroying that.
So we're also going to lose our score manager script once we destroy our player game object.
So that's why we just created a separate game object for that score manager script.
And we just attach.
Or you can just attach to any game object, make sure it's, uh, don't get destroyed.
So you can simply attach it right away.
So basically that is the thing.