- Home /
PlayerPrefs save after quit?
I am trying to save highscores for my motorcycle game. But when I close the app out of my iphone and reopen it once I score some points it overrites the highscore.This is only after I close the app completely. I am trying to display the player prefs after I get the value with a guitext. function Start () {
highscoretext.text = (PlayerPrefs.GetInt("Highscore")).ToString ();
function Update()
{
PlayerPrefs.Save();
if(score > high_score) {
high_score += 100;
highscoretext.text = "HIGHSCORE : " + high_score;
PlayerPrefs.SetInt("Highscore", high_score);
}
Would you be able to post the full script(s)? I can't really get a good idea form just that. Also I would avoid putting PlayerPrefs.Save() in the update function as that will cause performance problems.
PlayerPrefs should only be used where actually necessary, not every frame in Update.
Do you ever get a value for high_score
from PlayerPrefs? From what you've posted you're only setting the GUIText's text in Start(), not the high score itself.
Yes I get an int value from the high_score its a variable.
Answer by fafase · Oct 11, 2014 at 02:19 PM
I would say you do not really need to save in the Update. Since you are on iOS you can use:
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html
or:
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html
but make sure you read the bottom line of the page about the iOS not quitting.
Then you are getting the value on Start and again you do not need to check in Update, create a method to update the score value and at the same time check if bigger than the HiScore:
private var highScore:int;
private var score:int;
private var maxValue:int = 10000000;
function Start()
{
highScore = PlayerPrefs.GetInt("highscore");
highscoretext.text = highScore.ToString();
}
function AddScore(newScore:int)
{
if(newScore < 0)
{
Debug.LogError("Negative value?...Really?");
return;
}else if (newScore > maxScore)
{
Debug.LogError("That is a big score??!!");
return;
}
score += newScore;
if(score > highScore)
{
highScore = score;
highscoretext.text = highScore.ToString();
}
}
function OnApplicationPause(pauseStatus: boolean) // Or Quit
{
PlayerPrefs.SetInt("Highscore", high_score);
PlayerPrefs.Save();
}
Answer by 767_2 · Oct 11, 2014 at 01:59 PM
grab the save and set out of update and put it in your quit(exit) button
function exitFromGame()
{
if(currentScore>high_score)
PlayerPrefs.SetInt("Highscore", currentScore);
PlayerPrefs.Save();
}
Whenever the game is closed and reopened and I collect point from int vars it still overwrites the old highscore regardless of the score is higher than the highscore.
so you can check if your score is more than the highscore , i edited
Your answer
Follow this Question
Related Questions
How can i get the following script back on track to being a single level high score saver 0 Answers
Can someone help me with my highscore script? 2 Answers
Highscore won't save 3 Answers
Help with RPG 2 Answers
Making a less complicated save system. 2 Answers