- Home /
player prefs highscore
i have the highscore for my game sorted out but there is a catch. i have 2 modes and the high score in gamemode 1 is the same as 2 and vice versa. how can i fix this? I have the code and also in gamemode 1 i have the highscore text object called highscore compared to bestScore as i was trying to fix it.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class RushHighScore : MonoBehaviour
{
public int score = 0;
public int bestScore = 0;
private void Start()
{
bestScore = PlayerPrefs.GetInt("bestScore");
FinishedGame();
}
void FinishedGame()
{
print(bestScore);
if (score > bestScore)
{
bestScore = score;
PlayerPrefs.SetInt("bestScore", bestScore);
print("New High Score; " +
PlayerPrefs.GetInt("bestScore"));
}
}
}
Answer by I_Am_Err00r · Jul 23, 2019 at 09:24 PM
It sounds like you are naming bestScore the same in both games.
PlayerPrefs.SetInt("aGameBestScore", bestScore)
PlayerPrefsSetInt("bGameBestScore", bestScore)
That should assign bestScore in each game to the specific archived best score.
Oh no in game one it's get int high score for 2 it's bestScore
Are you sure you are not actually na$$anonymous$$g the string reference the same? You cant have it so that in game 1 you set the high score like this:
PlayerPrefs.SetInt("bestScore", highScore);
and then in game 2 set it like this:
PlayerPrefs.SetInt("bestScore", bestScore);
and expect them to be different; with that you are overwriting your original "bestScore" (set by game 1 as highScore) with the game 2 bestScore.
You would need to do something like I said earlier, and have two different string references to the PlayerPref variable, and then you can have just one int for best score because when you load the game, it will do this if game one:
bestScore = PlayerPrefs.GetInt("aGameBestScore");
and it will set the variable bestScore to the PlayerPref reference, and of course vice versa for the game 2 PlayerPref.
bestScore = PlayerPrefs.GetInt("bGameBestScore");
I hope that makes sense, but with PlayerPref you are creating a reference to an int with the string, and then calling that back at the load of a scene based on the same reference.