- Home /
PlayerPrefs isn't saving between Scenes
I've tried looking at a bunch of answers on here, but still having bad luck. I'm using PlayerPrefs to save a high score then switch scenes and show that high score. It actually works (sort of) - in play scene it stores the high score, then in game over scene it shows it. You have the option to restart from game over scene (SceneManager.LoadLevel) and it works, but after a few times it just doesn't show the same high score. It's almost like it resets itself. See play scene:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreTracker : MonoBehaviour {
PlayerMovement playerMove;
Text text;
public int points = 0;
public int highScore;
void Start()
{
playerMove = GameObject.Find("Player").GetComponent<PlayerMovement>();
text = gameObject.GetComponent<Text>();
}
// Update is called once per frame
void Update ()
{
points = playerMove.points;
text.text = "" + points;
if (points > highScore)
{
highScore = points;
PlayerPrefs.SetInt("High Score", highScore);
PlayerPrefs.Save();
}
}
}
and Game over scene script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class HighScoreTracker : MonoBehaviour
{
Text text;
public int currentScore;
void Start ()
{
text = GetComponent<Text>();
}
// Update is called once per frame
void Update ()
{
currentScore = PlayerPrefs.GetInt("High Score");
text.text = "" + currentScore;
}
}
And I have tried running Debug.Log after every playerpref, but it stops showing what it's saving... because it's actually working? Any ideas from anyone?
Answer by Eno-Khaon · Apr 25, 2016 at 11:26 PM
It looks like you're never assigning your saved high score to your highScore value.
When you create the public variable highScore, it's being initialized to 0. The first place you use it after that is when you check whether the current score is higher than it.
public int highScore;
// ...
if (points > highScore)
Because you're not also loading the high score into that variable, you're overwriting the old one with every fresh game session.
@$$anonymous$$o $$anonymous$$haon It seems as if I'm missing something here still. I know what you mean, just not sure how to implement. I wrote this, but it just made it even worse.
public class ScoreTracker : $$anonymous$$onoBehaviour
{
Player$$anonymous$$ovement player$$anonymous$$ove;
Text text;
public int points = 0;
public int highScore;
void Awake ()
{
PlayerPrefs.SetInt("High Score", highScore);
}
void Start()
{
player$$anonymous$$ove = GameObject.Find("Player").GetComponent<Player$$anonymous$$ovement>();
text = gameObject.GetComponent<Text>();
}
// Update is called once per frame
void Update ()
{
points = player$$anonymous$$ove.points;
text.text = "" + points;
if (points > PlayerPrefs.GetInt("High Score"))
{
points = highScore;
PlayerPrefs.SetInt("HighScore", highScore);
PlayerPrefs.Save();
}
}
}
Am I on to something here? Or still off completely?
void Awake ()
{
PlayerPrefs.SetInt("High Score", highScore);
}
Close, but also the exact opposite of the intended outcome. Try this ins$$anonymous$$d:
void Awake ()
{
highScore = PlayerPrefs.GetInt("High Score", 0);
}
I see what you mean and changed it (Thank you!), but it's returning a zero value in the game over scene oddly.
Your answer
Follow this Question
Related Questions
One PlayerPref not saving 1 Answer
Taking high score from anther script and then displaying it 2 Answers
How do I save highscore (Very Simple) 3 Answers
Score not resetting 3 Answers
PlayerPrefs not working with android 1 Answer