- Home /
how to save a highscore
i am making a android game and i have a int variable that i want to be saved if the game is quit so when you start playing again you still have you're highscoe. this is my code:
*#pragma strict
static var highscores : int = 0;
function Start () {
} function Update () {
if(highscore.score > highscores){
highscores = highscore.score;
} guiText.text = highscores.ToString();
}*
i want the variable highscores to be saved.
i can't get it to work is there anybody else who nows how to do this?
Answer by KiraSensei · Feb 23, 2014 at 06:00 PM
What you are looking for is THIS.
You can add a line of code in a method like OnApplicationExit (called when the player quits), or somewhere else if you prefer :
PlayerPrefs.SetInt("HighScore",highscores);
And then when you want to read it :
highscores = PlayerPrefs.GetInt("HighScore");
EDIT : I suggest you to NOT put these methods in the Update method !
thank you for you're reaction but what am i doing wrong?
#pragma strict
static var highscores : int = 0;
function Start () {
highscores = PlayerPrefs.GetInt("HighScore");
}
function Update () {
if(highscore.score > highscores){
highscores = highscore.score;
}
guiText.text = highscores.ToString();
}
function OnApplicationExit () {
PlayerPrefs.SetInt("HighScore",highscores);
}
In my opinion, nothing, everything should be O$$anonymous$$.
Be sure to quit the application correctly (not with the "play" button from Unity, but with some UI launching the call Application.Quit();
)
i can't get it to work can it be that it's diffrent on android?
It's the same. You need to create a trigger to allow the user to quit properly the application.
Your answer
Follow this Question
Related Questions
How to save data without using PlayerPrefs? 2 Answers
PlayerPrefs not working on android 2 Answers
PlayerPrefs file wrong stored location 0 Answers
General mobile game developing questions 0 Answers