hi guys,
I’m having some trouble in getting my app to record a high score. It’s essentially a “test” app - I have a number of individual scenes with questions on them, and I’m using a singleton to keep score as you progress through the different scenes/questions.
At the “End Screen” I have a ScoreConverter game object and script, which takes the currentscore in the singleton, converts it to your finalscore, and depending on what the finalscore is, gives a star rating / message etc.
The problem: Every time I run through the test, the “high score” i’m displaying is just the current tests score. It doesnt seem to persist the score between test sessions. I also need to make sure that this persist through usage of the app until it’s reset (still have to code this last part).
public class ScoreConverter : MonoBehaviour
{
[SerializeField] TextMeshProUGUI finalscore;
[SerializeField] TextMeshProUGUI percentResult;
[SerializeField] TextMeshProUGUI feedbackMessage;
public float YourScore;
float newHighScore;
public Text highScore;
void Start()
{
//Calculate the final score from the currentScore in the Singleton
YourScore = FindObjectOfType<GameStatus>().currentScore;
finalscore.text = (YourScore.ToString() + " out of 25");
//Save New High Scores
if (YourScore > PlayerPrefs.GetFloat("HighScore", 0))
{
newHighScore = YourScore;
PlayerPrefs.SetFloat("HighScore", newHighScore);
highScore.text = newHighScore.ToString();
}
// Covert the final score into a % based on 50 questions
float Percent = YourScore * 4;
percentResult.text = (Percent.ToString() + "%");
I should note that I’ve tried this a couple of different ways including without the newHighScore value. I followed the Brackeys YouTube tutorial and the unity documentation is a bit lite on PlayerPrefs so I reckon I must be missing something. Any help would be super appreciated.
Thanks,
MG