We also need to show up the high score that we made inside our game over UI.
So I'm gonna make my player health a little bit low for testing purposes.
So I'm gonna move over to my hierarchy and let's select this player game object.
Let's go to player Skip in Inspector.
And I'm going to make the max health something like one.
And I'm going to speed up.
I'm gonna make I'm gonna increase this speed of player game object something like five for testing purposes.
And let's click on this play button in the top.
By now you will see that our player game object just gonna move a little bit faster.
And the max health of player we set one.
So if we just take one damage right away this player game object just gonna loses all of his health
and then die function just gonna call right away.
So let's have a look on this and we can see player game object just died and the high score is set to
zero.
So we also need to, uh, need to show up the high score that we made met.
If our current score is greater than previous high score, then we're going to make that current score
as a new high score.
So let's get out of play mode and we're going to open up our score manager script.
Inside that script we're just doing those things.
So I'm going to select my Score Manager GameObject in the hierarchy.
And let's open up the Score Manager script quickly quickly by double clicking on it.
So what we basically need to have we need to have a reference of that high score text.
So if I move over to hierarchy, then canvas, then game over UI and we can see we have made a high
score.
As you can see, if I just enable this, we can see we need a reference of this zero text, basically
our high score.
So we need a reference of this text.
Then we can just show that up in our UI.
So let's go to script.
And we're going to make over here.
So let's make a another serialized field that will be private.
And the component we're looking for is a text type.
So we're going to type this text.
But before you type text make sure you just go to the top of your class and include this namespace called
using Unityengine.ui.
So whenever we're going to work with UI, make sure you just go to the top of your class and include
this namespace.
And I'm going to call this one something like Highscore text.
So let's call it high score high score text.
And let's close that up with semicolon.
And we're going to press down Ctrl S to save script.
And let's move to unity.
So we need to drag the reference in that slot underneath our score Manager script.
So just wait for to compile.
And we need to drag the reference in that slot.
Then we can just update the score in our high score.
So now if I select my Score Manager GameObject in the hierarchy underneath our Score Manager script,
we can see high score text and it says none text.
So what we're going to do, we're going to select this text.
And we're going to drag in that slot.
Our high score tech.
So I'm going to select my high score.
Let's drag it into that slot.
And now we can see we have the reference.
Basically we have this high high score text reference.
And we are ready to update our high score into this high score text.
So let's open up our script by quickly.
And I'm going to move over to this start function.
So start function get call in the start of the game.
And we do know once we start our game we're not going to have our score.
So what we're going to do we're going to update the text.
So we will move over to high score text.
And then we're going to access the text.
And we will set equal to the text player preps.
Basically we're going to use player preps in order to save uh our high score.
This is unity in build system in order to save, uh save our score.
And if you just want to jump into more complex, uh, then, uh, then you can just go to YouTube.
Uh, if you just want to save multiple datas, uh, then you should not use this one as, but for beginners
it's pretty much fine.
So basically what we're doing, we're just going over to this high score, basically this one and then
accessing this text as you can see.
And we're setting the text equal to.
So we're going to use this playerprefs.
So we need to use this Playerprefs.
And we do know in the start of the game we're not going to have any score.
So we're going to use this get float function and this get float function as a first argument.
It's going to take the key or which key you just want to save.
So we're going to create a key.
And you need to create the key in this app in between this quotation.
Because this is just going to have a string.
And I'm going to call it something like high score or, or all of these letters in capital letters and
comma.
And the second thing you need to set the value.
So once we start our game or we play our game for the first time, we're not gonna have any score.
So that's why we're going to put zero f.
And now we're going to close that off with a semicolon.
So this high score text dot text over here is a string parameter.
And this over here is a float parameter.
And we can see that we cannot implicitly convert type float to string.
So what we basically have to do.
So this is our float parameter.
So we need to convert this float into a string and into a string.
In order to convert we just have to use this two string function.
And it's going to convert that.
So we're doing inside this start function.
So start function get call in the start of the game.
So in the start of the game what we're doing we're just updating our Highscore text equal to Playerprefs
dot get float.
So we're using this function Playerprefs dot get float in order to get our default high score.
So and we also need to set the score by now.
So we're going to move over to this update function.
So basically what we're doing we're just going over to this Highscore text and then accessing this text.
And we're setting this text equal to this Playerprefs dot get getfloat and we're just saving or getting
the data from this high score key.
As you can see.
So later on we're going to save our data in this key.
And the default value we set it to zero.
So that's why this high score text dot text that's going to be zero right away.
So let's have a look.
So I'm gonna press Ctrl s to save my script.
And let's go to unity.
So once we start our game or we play our game you will see the default value of that high score just
gonna be zero because we're doing over here.
As you can see a gate float and we just giving the default value which is zero.
So let's go to unity now.
And I'm going to click on this play button in the top.
But before I do I'm going to deactivate this game over UI by default.
And let's click on this play button in the top.
So once our player game object just gonna die, you will see the high score just gonna be zero.
Because in the start of the game we're just getting kidding.
Giving a default value which is zero in our eyes.
Cortex.
So let's take damage and we can say high score is now zero.
And the current score we made so far is a 75.
So what we basically have to do, we need to set this as 75 or this current score as a new high score.
Because this current score we can see is greater than our previous high score, which is zero, or our
starting high score.
So we need to set this current score as a new high score because this current score is greater than
our previous high score.
So that's why we need to set this current score as a new high score.
So let's get out of play mode and let's go to update function.
Where we doing.
So we doing over here.
So we just updating our current score.
And also over here what we're going to do.
We're going to update the high score as well.
So we will set our current score as a new high score.
But before we set our current score as a new high score, we need to check if this current score is
greater than previous high score.
Let's suppose a previous high score is something like zero, and the current score we made five.
So we do know that our current score is greater than our previous high score.
Basically previous high score is zero and the current score is five.
And we do know that five is greater than zero.
So that's why that current score.
We need to set as a new high score.
So before we set the high score we're going to check over here.
So we're going to use this if condition and make first bracket and some curly brackets.
And what we want to check over here.
We just want to check.
So we're going to take our current score.
And we will check if if our current score is greater than.
So we're going to use this Playerprefs in order to get the default value or default.
Uh sorry previous high score.
So we need to use this Playerprefs.
And we're going to use this get float function and this get float function.
It's going to take as a first argument the key and which key?
You just saving your data.
So in our case we're gonna type this high score.
So make sure you just spelling it correct.
Because we're just getting the default value in this high score key.
As you can see in this start function.
So we're going to save our data.
Or we're going to get the previous high score by using this high score key.
Or where we're saving.
So high score and the default value is just going to be zero or the previous score.
Or you can just leave it like this.
It's basically means what we're doing.
We're just checking if our current score is greater than Playerprefs dot get float.
High score basically means previous high score.
So if it's greater than then we need to set this current score as a new high score.
Because this current score is greater than our previous high score and the high score we're getting
from this high score key, as you can see in between this question.
So make sure you just put in between this quotation and you spell it correct.
The key we key in which key you just are getting or setting the value.
And now what we have to do.
We need to set this current score as a new high score, because this current score is greater than our
previous high score.
And in order to set this current score as a new high score, what we have to do, we need to use this
Playerprefs function.
So we're going to use this Playerprefs.
And to set the parameter or this current score as a new high score, we need to use this set float function.
And this set float function as a first argument.
It's going to take the key in which key you just want to save your data.
So we're going to save our data in this high score key.
As you can see.
So we're going to make first a quotation.
And then we're going to pass the key which is our high score.
And the second thing you need to pass the score that you want to set.
So we're going to pass this current score Y is going to set the y y.
We're going to pass this current score.
Because this current score as you can see is greater than our previous high score.
So that's why we just want to set this current score as a new high score.
So we're going to pass our current score as a second parameter.
And now we're going to close that off with semicolon.
And as you can see firstly we're checking if our current score is greater than our previous high score.
So let's suppose the current score is something like two.
And the previous high score is one.
And we're checking over here as you can see.
So this one is two.
So two greater than one.
So so what we're doing in in that moment we're just setting that current score as a new high score.
Basically current score is now two.
So we're setting this two as a new high score.
As you can see we're using this set float function in order to set the new high score.
So we just assume it to but it can be any number.
And after that what we have to do we need to update our high score text as well.
So if we move over to unity and then set this game over UI, we just want to update that current score
as a new high score.
So we also need to, uh, show up in our UI that how much score we made so far or that score as a new
high score.
So we also need to update this high score text with our new high score.
So what we will do, we will go to script.
And after setting that current score as a new high score, we're going to update in the UA as well.
So we will go to high score text.
And now we're going to access the text.
And we will set equal to text our current score.
Because this current score by now as a new high score.
And let's close that up with semicolon.
So this current score we can see is a float parameter.
And this one high score dot text dot text is a string parameter.
So we need to convert this float parameter into a string.
And to convert that we just basically have to use this dot two string.
And that's it.
And now we're going to press Ctrl S to save our script.
And let's move over to unity.
And you will see that.
So as soon as our current score is going to be a new, uh, basically greater than our previous high
score.
Than current score.
Just going to be a new high score.
So basically what we're doing, we're just going over to this, uh, sorry, this high score and then
accessing this text of this high score, and we're setting this text equal to our new current score,
the high score we set as a new high score, which is current score, as you can see.
So I hope you get the idea of this.
And if you still a little bit confused then of course I will give a another video link in the description,
uh, description as well.
So sorry, I'm not gonna put it in description, but I'm gonna give a, I'm gonna give in these resources
the link so you can go and check that out if you just want to learn more deeply.
And let's go to unity and make sure you just press down Ctrl S to save your script and then move to
unity.
And by default, I'm gonna deactivate or just gonna set active to false this game over and let's have
a look.
So I'm going to click on this play button in the top to play my game, and let's see if the high score
just getting updated as a new high score.
So if our current score is greater than previous high score, then that current score is just going
to be set as a new high score.
So let's have a look on this.
So oh okay.
So we so I'm going to get out of play mode.
And if I just play click on this play button in the top.
You will see that our current score is just going to be a new high score.
Uh, I guess we met around 93.
Uh, so let's have a look.
So I'm going to take damage from this enemy game object and you will see.
And we can see that high score is 93 because we previously made 93.
Our high score and the current score is we can see is 75.
So that's why this current score is not set to new high score.
Basically this current score is not greater than our previous high score, which is 93.
And we do know that 75 is not greater than 93.
So that's why this current score not set it to as a new high score.
But if the current score is greater than greater than our previous high score, you will see the high
score just going to be updated with our new, uh, basically new score, new high score.
So let's get out of play mode and let's make a number or score something around 100, because that 100
is just going to be greater than 93.
And we should see that current score that we made which is 100 or greater than 100 number.
Uh, it's going to save as a new high score.
So let's check that out.
So I'm going to get out of play.
I'm going to click on this play button in the top in order to check it one more time.
So let's have a look.
And we're going to do that I'm going to take damage.
Sorry.
And and we should see.
So we made our current score something like 130 as you can see, or 150.
So I'm going to get out of play mode.
Now.
This current score should be as a new high score.
So I'm going to click on this play button in the top by now.
And I'm going to die.
And we should see that current score that we made which is 150 should be as a new high score because
that number is our greater than previous high score, which is which was 93.
And we do know 150 is greater than 93.
So it's going to be saved as a new high score.
And we can indeed see that high score is 167 as we made.
And the current score is 76, and the 76 is not a greater than our previous high score or uh, that
93.
So that's why 167 is just set it to as a new high score.
So I'm going to get out of play mode.
But before I get out of play mode, we can see our high score is safe or this text just gets updated
with decimal places.
So to get rid of this decimal places, what we basically have to do in quotation, we just need to pass
zero.
So let's get out of play mode.
And if we move over to unity.
So we're just setting as you can see two string.
So over here what we basically have to do once we convert this float parameter into a string, you have
to put this quotation.
And in this code between this quotation you just need to pass the zero f.
So this is just going to be ah.
So it will just ignore those decimal places or that floating numbers and it's gonna show up the whole
numbers instead.
Now I'm going to press down Ctrl s to save my script.
And let's move over to unity one more time.
And we should be able to see we should see that those decimal places are just gonna be out, uh, from
our high score text.
So let's have a look on this.
So I'm going to click on this play button in the top by now.
And let's have a look on this.
Is.
So let's take damage from that anime game object.
This anime game object.
So one two and we can see we still able to see high score one, six and seven.
Basically what we have to do, we need to get out of play mode.
And we also need to make this one in the start of the game.
As you can see, we just sitting.
So once we convert this float parameter into a string, we need to put inside this quotation as well.
Then zero.
So make sure you just do that.
And I'm going to remove this f instead.
So make sure you once you're converting a float parameter or a integer parameter into a string, make
sure in between this quotation you just pass zero.
So it's going to ignore the decimal places.
So I'm going to press down ctrl s one more time in in order to save my script.
So we need to do both of those sites and we forgot to do or it was left behind.
So that's why we need to move over to script, and we need to put it in quotation zero as well to that.
So I'm going to click on this play button in the top.
And it should be fine by now.
If it's not uh then uh I guess it should be fine.
So you can go with decimal places as well.
It's totally up to you.
Uh, but before we do, let's have a look.
So we gonna ignore those decimal places or those floating numbers as soon as we're gonna have.
So we can see the high score by now is 168, uh, 168.
And it's not, uh, with our decimal places, so we can only see our whole numbers.
Whole numbers like one, six, eight, naught point and the blah, blah.
So basically that is the thing.
And I'm going to get out of play mode.
And let's suppose you just want to delete the high score you made.
Uh, made you just want to reset that.
So what you have to do.
So we're gonna get out of play mode.
And in order to do that, we're going to move over to script and we're going to make a function in the
bottom something over here.
And for function we're going to type void.
And we have to name the function.
So I'm going to call it gonna call I'm going to call it uh delete delete high score something like that
I'm going to call that function.
And let's make first bracket and some curly brackets.
So what I want to do inside this function, I just want to delete the high score I just made.
Or I just want to reset the high score.
So in order to reset the high score you need to use this player preps player preps dot delete all.
So if you use delete all then first bracket and then close it up with semicolon.
It will delete all of the saving data.
So let's suppose you uh use another thing to save a besides uh, as besides with this high score as
well, you just saving another more data.
It can be something like, uh, health mana or something like that.
It was also just going to be, uh, delayed.
But let's suppose you just want to delete specific, uh, data, something like this high score.
Then you can use this, uh, delete key function.
Delete key function.
And this function, it's back.
It's going to take the key that you want to delete.
So I'm going to pass delete key and make sure you making first packets.
And in this quotation in brackets in a string you need to pass the key in which key you just saved your
data.
So in our case we're going to pass in quotation high score.
So I want to delete the high score.
So that's why you need to put the key in between this quotation in which key you saving data.
So in my case I'm just saving my data in this high score.
And I just want to delete my high score or want to reset my high score.
So that's why we need to pass in in quotation the key and make sure you just close that up with semicolon.
And also after deleting the key, we need to update in this high score as well.
Or that high score we uh, we seen.
So we also need to update that.
So after deleting we're going to say high score, high score text dot text.
And we're going to set equal to zero as zero.
So make sure you just pass this quotation and then pass this zero inside this quotation.
Because this uh this right over here is a string parameter.
And once you put anything in between this quotation, it's going to be string.
So that is the reason.
So make sure you just do that.
And we also need to call this uh delete score function.
And what this score function just gonna do.
It's gonna delete the key or in which key we just saved our data.
So in my case high score.
So we just say deleting.
And after that we just also updating in the UI saying zero.
As you can see simply we just going over to that high score text this basically this high score And
then accessing this text.
And we're setting this text equal to zero.
As you can see.
So it's going to be update.
And I want to check something like once I click right mouse button I just want to call this delete high
score function.
And this delete high score function is just going to do these things.
So I'm going to call it over here inside this update function.
And I'm going to check.
So I'm going to check for some input.
And I want to check over here.
So I'm going to pass this input dot get mouse button down.
And over here in brackets I'm going to pass one.
One basically means the right mouse button means if we're clicking the right mouse button then what
I'm going to do I'm going to call this delete high score function.
And this delete high score function is just going to delete this data in which as a key we just store
it.
So in our case high score.
So I'm going to call the high score delete delete high score or delete score once I click the right
mouse button.
So I'm going to call the function.
And let's make first bucket.
Let's close that off with semicolon.
And we're going to press down Ctrl S to save our scroll to save our script.
And let's move to unity.
So whenever we're clicking the right mouse button then what we're doing we're just going or just calling
the delete Highscore function.
And what delete high score function is doing.
It's just deleting our high score.
And after that we just updating the UI which is high score with zero as you can see.
So let's go to unity now and let's click on this play button in the top.
You will see as soon as we're going to click right mouse button uh the saving data will be deleted.
And after that we're just updating that high score with zero.
Once we delete, uh, those uh, basically the saving data you will see.
So just wait for it to come take damage and we can see it's deleted.
Our player game object died.
And if I just right click so the high score we can see 168.
And as soon as we can right click.
So if I just right click.
So basically what we have done.
So I'm going to get out of play mode.
Uh over here as you can see if Game manager is uh instant dot is game active is false.
Basically means once we, uh, once our player game object is died, then we just, uh, typing return
and after return, whatever we're going to do, uh, it's not going to be saved.
So what we're going to do, we're going to put this line of code by Ctrl X, and we're going to put
it inside this control, uh, v by pasting it.
So once we, uh, when we're doing these things, basically, uh, once our player game object destroy,
then we just, uh, and we also checking, uh, sorry.
Uh, uh, I'm going to put a over here, so I'm going to put in uh, top.
So basically just put this line of code in top of your update function because after return we don't
know it's not going to be work.
So that's why we just put it, uh, before this thing.
So I'm going to press down Ctrl S to save my script.
And let's press down Ctrl s.
Let's move to unity.
So whenever we're going to click right mouse button by now it should be deleted because we're just putting
that line of code before this before our player game object dies.
And we do know after return whatever we're going to do it's not going to be executed.
So that's why we just put that line of code before return or that game manager instance, uh, line
of code.
So just wait for to compile and we're going to able to see by now.
So let's click on this play button in the top and let's have a look.
And let's take damage from that enemy game object which is our skeleton.
So let's take damage one two.
And we can see the high score is 168.
And if I just right click.
As you can see, as soon as we right click the high score, just set it to zero.
And we just deleted the saving data as well.
And we just update in the UI.
So basically what we're doing we're just going over to this high score then accessing this text.
Once we deleting our score.
And we're setting this text equal to over here as you can see zero.
So that's why in the UI this also gets updated with zero.
So basically that is the thing.
And I'm gonna get out of play mode.
I hope you get the idea that how you can show up the high score that you made, the current score,
you just want to set as a new high score.
So you can do that.
So you can use Playerprefs in order to, uh, save specific data.
And if you just want to save multiple datas, then you can, uh, check that out on YouTube.
Uh, so you can just go do that if you just want to do it.
So I'm going to press Ctrl s on my keyboard to save my project.