✅ 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
TheRanger
TheRanger2y ago
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
SteliosKrom
SteliosKromOP2y ago
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
Chiyoko_S
Chiyoko_S2y ago
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
Cattywampus
Cattywampus2y ago
If it's just a highscore and nothing else and if your game just a single player. You can just save it to PlayerPrefs
//Save
PlayerPrefs.SetInt(KeyName, Value);

//Load
var highScore = PlayerPrefs.GetInt(KeyName);
//Save
PlayerPrefs.SetInt(KeyName, Value);

//Load
var highScore = PlayerPrefs.GetInt(KeyName);
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 it
SteliosKrom
SteliosKromOP2y ago
What I did is that:
public void CheckSaveUpdateHighScore()
{
if (score > highScore)
{
highScore = score;
StoreHighScore();
}
highScoreText.text = highScore.ToString();
}


public void StoreHighScore()
{
PlayerPrefs.GetInt("highScore", highScore);
PlayerPrefs.Save();
}
public void CheckSaveUpdateHighScore()
{
if (score > highScore)
{
highScore = score;
StoreHighScore();
}
highScoreText.text = highScore.ToString();
}


public void StoreHighScore()
{
PlayerPrefs.GetInt("highScore", highScore);
PlayerPrefs.Save();
}
but this doesn't save the highscore, whenever I want to restart my game. It just gets back to 0 in gamemanager
Cattywampus
Cattywampus2y ago
you didn't assign the return value there, so it's expected also you should store it 1st see my snippet above
SteliosKrom
SteliosKromOP2y ago
Do i have to return a value?
Cattywampus
Cattywampus2y ago
GetInt returns int as the name implies
SteliosKrom
SteliosKromOP2y ago
I did that here in the startgame method
if (PlayerPrefs.HasKey("highScore"))
{
highScore = PlayerPrefs.GetInt("highScore");
}
if (PlayerPrefs.HasKey("highScore"))
{
highScore = PlayerPrefs.GetInt("highScore");
}
Cattywampus
Cattywampus2y ago
GetInt is to get... not saving you should save it 1st
MODiX
MODiX2y ago
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
//Save
PlayerPrefs.SetInt(KeyName, Value);

//Load
var highScore = PlayerPrefs.GetInt(KeyName);
//Save
PlayerPrefs.SetInt(KeyName, Value);

//Load
var highScore = PlayerPrefs.GetInt(KeyName);
React with ❌ to remove this embed.
SteliosKrom
SteliosKromOP2y ago
The problem is that I don't know where to save it
Cattywampus
Cattywampus2y ago
when your game about to end/gameover ideally
SteliosKrom
SteliosKromOP2y ago
I check the highscore and save it as well I call the function
Cattywampus
Cattywampus2y ago
show the code where you save it?
SteliosKrom
SteliosKromOP2y ago
In the game over method
Cattywampus
Cattywampus2y ago
yeh, show the code
SteliosKrom
SteliosKromOP2y ago
public void DestroyGoodOutOfBounds()
{
if (transform.position.y < -yBound && !gameObject.CompareTag("Bad") && !gameManager.gameOver)
{
gameManager.gameOver = true;
Destroy(gameObject);
gameManager.StopPlayerAudioOnGameOver();
gameManager.GameOver();
gameManager.GameOverSound();
gameManager.CheckSaveUpdateHighScore();
gameManager.highScoreText.text = "HighScore: " + gameManager.highScore;
Debug.Log("Good objects destroyed the player audio stop on game over and game over sound enabled");
}

}
public void DestroyGoodOutOfBounds()
{
if (transform.position.y < -yBound && !gameObject.CompareTag("Bad") && !gameManager.gameOver)
{
gameManager.gameOver = true;
Destroy(gameObject);
gameManager.StopPlayerAudioOnGameOver();
gameManager.GameOver();
gameManager.GameOverSound();
gameManager.CheckSaveUpdateHighScore();
gameManager.highScoreText.text = "HighScore: " + gameManager.highScore;
Debug.Log("Good objects destroyed the player audio stop on game over and game over sound enabled");
}

}
I call it in the other script where my game goes game over
Cattywampus
Cattywampus2y ago
That means nothing unfortunately, show us the body of the saving function instead.. where you do PlayerPrefs.SetInt
SteliosKrom
SteliosKromOP2y ago
I show you above
Cattywampus
Cattywampus2y ago
I see no SetInt there 😐
SteliosKrom
SteliosKromOP2y ago
Maybe, I have to call the StoreHighScore() to the other script where it does game over?
Cattywampus
Cattywampus2y ago
Are you using assets or template from other game or?
SteliosKrom
SteliosKromOP2y ago
template basically Only I got some sound effects and particle effects from asset store
Cattywampus
Cattywampus2y ago
show me what's in gameManager.CheckSaveUpdateHighScore() show me the code in there
SteliosKrom
SteliosKromOP2y ago
Here it is
Cattywampus
Cattywampus2y ago
Yeah, that does nothing to save it
SteliosKrom
SteliosKromOP2y ago
Do i have to setint like you said? I do playerprefs.save()
Cattywampus
Cattywampus2y ago
show me the StoreHighScore method
SteliosKrom
SteliosKromOP2y ago
It is in the same
Cattywampus
Cattywampus2y ago
oh yeah it does nothing to save
SteliosKrom
SteliosKromOP2y ago
So, what I have to do? Do I have to use set int? I only do getint not that you said
Cattywampus
Cattywampus2y ago
show me what' show me what's in Save
SteliosKrom
SteliosKromOP2y ago
That's what in save()
Cattywampus
Cattywampus2y ago
oh my bad.... yeah
SteliosKrom
SteliosKromOP2y ago
SteliosKrom
SteliosKromOP2y ago
Here i check and call the storehighscore to save
Cattywampus
Cattywampus2y ago
change it to this
public void CheckSaveUpdateHighScore()
{
if (score > highScore)
{
highScore = score;
StoreHighScore();
}
highScoreText.text = highScore.ToString();
}


public void StoreHighScore()
{
PlayerPrefs.SetInt("highScore", score);
PlayerPrefs.Save();
}
public void CheckSaveUpdateHighScore()
{
if (score > highScore)
{
highScore = score;
StoreHighScore();
}
highScoreText.text = highScore.ToString();
}


public void StoreHighScore()
{
PlayerPrefs.SetInt("highScore", score);
PlayerPrefs.Save();
}
SteliosKrom
SteliosKromOP2y ago
Alright, so to change get int to set int right?
Cattywampus
Cattywampus2y ago
no wait not done give me a sec
SteliosKrom
SteliosKromOP2y ago
ok After you send me the changes that i have to make. Can you please tell me the difference between GetInt and SetInt?
SteliosKrom
SteliosKromOP2y ago
It says that but I don't quite understand it.
Cattywampus
Cattywampus2y ago
try that yeah I wasn't finished before, need to do somethings.. now try the updated
SteliosKrom
SteliosKromOP2y ago
Yeah that works
Cattywampus
Cattywampus2y ago
it works or not, im confused 😃
SteliosKrom
SteliosKromOP2y ago
But still the problem is that whenever i restart it doesn't show me the highscore It shows the 0 It shows 0
Cattywampus
Cattywampus2y ago
you need to load it back to your UI just do the reverse... via GetInt
SteliosKrom
SteliosKromOP2y ago
Yes in another method? In the startgame basically
Cattywampus
Cattywampus2y ago
no need just show the entirety of the class that contains the method I just modified
SteliosKrom
SteliosKromOP2y ago
This is where the game starts check what i did
Cattywampus
Cattywampus2y ago
paste that code here what is UpdateScore(0)? show me the code for that method too
SteliosKrom
SteliosKromOP2y ago
I update the score ok
SteliosKrom
SteliosKromOP2y ago
Cattywampus
Cattywampus2y ago
? yeah, you need to pass your highScore instead of 0 so UpdateScore(highScore)
SteliosKrom
SteliosKromOP2y ago
with a parameter i assume oh not the score? Understood Yeah that was the problem Thanks bro! That was very helpful
Cattywampus
Cattywampus2y ago
nice, now you can close this
SteliosKrom
SteliosKromOP2y ago
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!
Cattywampus
Cattywampus2y ago
close this thread if it's solved
SteliosKrom
SteliosKromOP2y ago
How? Close post
Cattywampus
Cattywampus2y ago
@Pobiega Sorry for pinging, but I don't know the command for closing 😃
SteliosKrom
SteliosKromOP2y ago
I think close post it says up there
Cattywampus
Cattywampus2y ago
proly @LLVM knows how to close answered thread? sorry for the ping too oh well, just leave it then awesome
TheRanger
TheRanger2y ago
/close like that
Buddy
Buddy2y ago
closed blobthumbsup
Cattywampus
Cattywampus2y ago
noted
TheRanger
TheRanger2y ago
i think u opened it again by posting here
Cattywampus
Cattywampus2y ago
oh shit pardon pls close it again when
Want results from more Discord servers?
Add your server