- Home /
I'm trying to set a high score but I can't display it in another scene?
I tried using this code to save a single high score but I'm having trouble using it.
This is for storing and displaying the score on the stage:
void Update () {
horizontalRatio = Screen.width / BASE_WIDTH;
verticalRatio = Screen.height / BASE_HEIGHT;
guiText.text = "Score: " + score;
guiText.fontSize = Mathf.Min(BASE_HEIGHT,BASE_WIDTH)/FSize;
}
public static void StoreHighscore(int newHighscore)
{
int oldHighscore = PlayerPrefs.GetInt("score", 0);
if(newHighscore > oldHighscore)
PlayerPrefs.SetInt("score", newHighscore);
PlayerPrefs.Save();
}
And this is my script for the display of the high score in the high score scene:
void Start () {
}
// Update is called once per frame
void Update () {
horizontalRatio = Screen.width / BASE_WIDTH;
verticalRatio = Screen.height / BASE_HEIGHT;
// var scoreposx = horizontalRatio / ScoreX;
// var scoreposy = verticalRatio / ScoreY;
guiText.text = "Score: " +PlayerPrefs.GetInt("newHighscore");
guiText.fontSize = Mathf.Min(BASE_HEIGHT,BASE_WIDTH)/FSize;
}
There aren't any errors showing up but I can't seem to make the high score show. I'm not even sure if the high score was saved.. I know this is all very basic and I've tried researching on the net and even youtube but I can't seem to find a way to make this work. Can anyone help me?
Answer by zharik86 · Jul 10, 2014 at 06:22 PM
You save your score in variable by name "score":
PlayerPrefs.SetInt("score", newHighscore);
But, you show on screen variable by name "newHighscore":
guiText.text = "Score: " +PlayerPrefs.GetInt("newHighscore"); //value 0 default
Change this line how:
guiText.text = "Score: " +PlayerPrefs.GetInt("score");
That will work.
I tried it now bu it's still the same. In the first code I store the active score in score that's why:
guiText.text = "Score: " + score;
Do I have to separate it from:
int oldHighscore = PlayerPrefs.GetInt("score", 0);
Like maybe turn it into:
int oldHighscore = PlayerPrefs.GetInt("highscore", 0);
PlayerPrefs.GetInt() only accepts one string value as its parameter. So use
int oldHighScore = PlayerPrefs.GetInt("score");
It also doesn't work.. Do I have to call the script or something for the display scene?
Answer by Tehnique · Jul 10, 2014 at 06:24 PM
You set the key as "score" and then retrieve it as "newHighscore". Replace line 14 with guiText.text = "Score: " +PlayerPrefs.GetInt("score");
I tried that but it's the same. Do I have to call the other script in the display to make it work?