- Home /
PlayerPrefs highscore system?
Hi! I'm trying to make a highscore system and I read a lot of questions here and it's almost done. I'm using this example: http://answers.unity3d.com/questions/20773/how-do-i-make-a-highscores-board.html
I have this:
if(GUILayout.Button("Salvar pontuacao!")){
PlayerPrefs.SetString("High Name", name);
PlayerPrefs.SetInt("High Score", score);
Application.LoadLevel(2);
}
And trying to show it in a GUI with these codes:
void AddScore(string name, int score){
newScore = score;
newName = name;
for(int i=0; i<10; i++){
if(PlayerPrefs.HasKey(i+"High Score")){
if(PlayerPrefs.GetInt(i+"High Score") < newScore){
//se o newScore for maior que o antigo
oldScore = PlayerPrefs.GetInt(i+"High Score");
oldName = PlayerPrefs.GetString(i+"High Name");
PlayerPrefs.SetInt(i+"High Score", newScore);
PlayerPrefs.SetString(i+"High Name", newName);
newScore = oldScore;
newName = oldName;
}
}else{
PlayerPrefs.SetInt(i+"High Score", newScore);
PlayerPrefs.SetString(i+"High Name", newName);
newScore = 0;
newName = "";
}
}
}
Here:
firstName = PlayerPrefs.GetString("0High Name", newName);
firstScore = PlayerPrefs.GetInt("0High Score", newScore);
secondName = PlayerPrefs.GetString("1High Name", newName);
secondScore = PlayerPrefs.GetInt("1High Score", newScore);
thirdName = PlayerPrefs.GetString("2High Name", newName);
thirdScore = PlayerPrefs.GetInt("2High Score", newScore);
fourthName = PlayerPrefs.GetString("3High Name", newName);
fourthScore = PlayerPrefs.GetInt("3High Score", newScore);
fifthName = PlayerPrefs.GetString("4High Name", newName);
fifthScore = PlayerPrefs.GetInt("4High Score", newScore);
sixthName = PlayerPrefs.GetString("5High Name", newName);
sixthScore = PlayerPrefs.GetInt("5High Score", newScore);
seventhName = PlayerPrefs.GetString("6High Name", newName);
seventhScore = PlayerPrefs.GetInt("6High Score", newScore);
eighthName = PlayerPrefs.GetString("7High Name", newName);
eighthScore = PlayerPrefs.GetInt("7High Score", newScore);
ninthName = PlayerPrefs.GetString("8High Name", newName);
ninthScore = PlayerPrefs.GetInt("8High Score", newScore);
tenthName = PlayerPrefs.GetString("9High Name", newName);
tenthScore = PlayerPrefs.GetInt("9High Score", newScore);
GUI.Box(new Rect(Screen.width/2 - 50, 50, 100, 20), firstName);
GUI.Box(new Rect(Screen.width/2 - 50, 100, 100, 20), secondName);
GUI.Box(new Rect(Screen.width/2 - 50, 150, 100, 20), thirdName);
GUI.Box(new Rect(Screen.width/2 - 50, 200, 100, 20), fourthName);
GUI.Box(new Rect(Screen.width/2 - 50, 250, 100, 20), fifthName);
GUI.Box(new Rect(Screen.width/2 - 50, 300, 100, 20), sixthName);
GUI.Box(new Rect(Screen.width/2 - 50, 350, 100, 20), seventhName);
GUI.Box(new Rect(Screen.width/2 - 50, 400, 100, 20), eighthName);
GUI.Box(new Rect(Screen.width/2 - 50, 450, 100, 20), ninthName);
GUI.Box(new Rect(Screen.width/2 - 50, 500, 100, 20), tenthName);
GUI.Box(new Rect(Screen.width/2 + 100, 50, 50, 20), firstScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 100, 50, 20), secondScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 150, 50, 20), thirdScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 200, 50, 20), fourthScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 250, 50, 20), fifthScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 300, 50, 20), sixthScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 350, 50, 20), seventhScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 400, 50, 20), eighthScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 450, 50, 20), ninthScore.ToString());
GUI.Box(new Rect(Screen.width/2 + 100, 500, 50, 20), tenthScore.ToString());
Please, I already try a lot of things but I'm stuck. Thanks!
It doesn't work. The score and name just show up in the GUI if I put a number here:
if(GUILayout.Button("Salvar pontuacao!")){
PlayerPrefs.SetString("0High Name", name);
PlayerPrefs.SetInt("0High Score", score);
Application.LoadLevel(2);
}
Doing it the score show at the first position. But if I play again the new score will show at the first position too.
one thing that catches my eye is that GetString and GetInt only take one argument but you pass two. Also I'd suggest to store these in an array ins$$anonymous$$d of having 10 single variables. You could define a class which contains a name and a score then make an array of this class. If you pack the array in another class, you can save it easily with this little skrip.
If you do this the first score gets overridden no matter what. I kinda assumed you would call your AddScore function at some place. Have the Button call this function ins$$anonymous$$d of onlyl overriding the first score.
In this case you can do it with a public static function. Declare AddScore as static and public:
public static void AddScore(string name, int score){
//your code
}
The you can call it from everywhere by using:
ranking.AddScore(someString, someInt);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I'm trying to set a high score but I can't display it in another scene? 2 Answers
Distribute terrain in zones 3 Answers