✅ How to make HighScore and Save it in reset in Unity?
Can you please help me make HighScore to be saved after the restart of my game?
67 Replies
u mean when u restart in game itself or when you close it and reopen it?
you need to save your highscore somewhere
in a file, or in a database server,
or even a local database file
you can use System.IO.File class to read and write data to a file
I mean I want to save my high score and when I restart the game and play again, I want my previous progress to be saved.
For one reason, when I play the game after restart it shows to me 0 and not my high score that I did before
What TheRanger said. You need to store the high score as a file somewhere - data stored in memory gets wiped once the program is closed
You could do it this way:
When the high score is being stored you also store it in a file somewhere (next to the program is one place you could store it to)
then, on application startup, you read from the said high score file to restore the last record
If it's just a highscore and nothing else and if your game just a single player.
You can just save it to PlayerPrefs
remember, just for simple stuff.. other than that you can use JsonUtility in Unity to serialize it as json
It is recommended to use
Application.dataPath
or the persistent
version with itWhat I did is that:
but this doesn't save the highscore, whenever I want to restart my game. It just gets back to 0
in gamemanager
you didn't assign the return value there, so it's expected
also you should store it 1st
see my snippet above
Do i have to return a value?
GetInt returns
int
as the name impliesI did that here in the startgame method
GetInt is to get... not saving
you should save it 1st
SlimStv#2835
If it's just a highscore and nothing else and if your game just a single player.
You can just save it to PlayerPrefs
Quoted by
<@!726512159940411422> from #How to make HighScore and Save it in reset in Unity? (click here)
React with ❌ to remove this embed.
The problem is that I don't know where to save it
when your game about to end/gameover
ideally
I check the highscore and save it as well
I call the function
show the code where you save it?
In the game over method
yeh, show the code
I call it in the other script where my game goes game over
That means nothing unfortunately, show us the body of the saving function instead.. where you do
PlayerPrefs.SetInt
I show you above
I see no SetInt there 😐
Maybe, I have to call the StoreHighScore() to the other script where it does game over?
Are you using assets or template from other game or?
template basically
Only I got some sound effects and particle effects from asset store
show me what's in
gameManager.CheckSaveUpdateHighScore()
show me the code in thereHere it is
Yeah, that does nothing to save it
Do i have to setint like you said?
I do playerprefs.save()
show me the
StoreHighScore
methodIt is in the same
oh
yeah it does nothing to save
So, what I have to do? Do I have to use set int?
I only do getint
not that you said
show me what'
show me what's in
Save
That's what in save()
oh my bad.... yeah
Here i check and call the storehighscore to save
change it to this
Alright, so to change get int to set int right?
no wait
not done give me a sec
ok
After you send me the changes that i have to make. Can you please tell me the difference between GetInt and SetInt?
It says that but I don't quite understand it.
try that
yeah I wasn't finished before, need to do somethings.. now try the updated
Yeah that works
it works or not, im confused 😃
But still the problem is that whenever i restart it doesn't show me the highscore
It shows the 0
It shows 0
you need to load it back to your UI
just do the reverse... via GetInt
Yes in another method?
In the startgame basically
no need just show the entirety of the class that contains the method I just modified
This is where the game starts check what i did
paste that code here
what is
UpdateScore(0)
?
show me the code for that method tooI update the score ok
?
yeah, you need to pass your highScore instead of
0
so UpdateScore(highScore)
with a parameter i assume
oh
not the score?
Understood
Yeah that was the problem
Thanks bro! That was very helpful
nice, now you can close this
That was the first time I was working with highscore, I am a beginner 5-6 months experience of programming c# and unity
Yeah, thanks!
close this thread if it's solved
How?
Close post
@Pobiega Sorry for pinging, but I don't know the command for closing 😃
I think close post
it says up there
proly @LLVM knows how to close answered thread? sorry for the ping too
oh well, just leave it then
/close
like that
closed
noted
i think u opened it again by posting here
oh shit
pardon
pls close it again