- Home /
PlayerPrefs not saving after i quiet application
Hi! so i have a playerpreffs thats not saving after i quiet the application and one that does, the code of them look the same (to me), and wondering why its not saving
{
private GameObject StartPos;
private GameObject PlayerPos;
public Text scoreText;
public Text hiScoreText;
public static float scoreCount;
public static float hiScoreCount;
private float yDistance;
public static float statScore;
public static bool justDied = false;
public static float NewStatScore;
public float pointsPerSecond;
public bool scoreIncreasing;
private void Start()
{
StartPos = GameObject.FindGameObjectWithTag("StartPos");
PlayerPos = GameObject.FindGameObjectWithTag("Player");
if (PlayerPrefs.HasKey("HighScore"))
{
hiScoreCount = PlayerPrefs.GetFloat("HighScore");
}
if (PlayerPrefs.HasKey("TotalHeight"))
{
NewStatScore = PlayerPrefs.GetFloat("TotalHeight");
}
}
private void Update()
{
if (scoreIncreasing && playerController.dead == false)
{
// points per second
yDistance = PlayerPos.transform.position.y - StartPos.transform.position.y;
scoreCount = pointsPerSecond * yDistance;
}
if (scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
PlayerPrefs.SetFloat("HighScore", hiScoreCount);
}
// print out on screen
scoreText.text = "Score: " + Mathf.Round(scoreCount) + "m";
hiScoreText.text = "High Score: " + Mathf.Round(hiScoreCount) + "m";
if (justDied == true)
{
NewStatScore = NewStatScore + scoreCount;
PlayerPrefs.SetFloat("TotalHeight", NewStatScore);
justDied = false;
}
}
}
here i have 2 player preffs. one thats TotalHeight and one thats Highscore. The highscore one is saving while the TotalHeight isnt. any tips?
New attempt. It must be unrelated to PlayerPrefs. Could you show us where you set "justDied" to true ?
Answer by SirPaddow · Aug 26, 2019 at 08:01 PM
Are you sure the highScore is saved after you quit? You have to call this function to actually save the PlayerPrefs between sessions :
PlayerPrefs.Save();
do i need a PlayerPrefs.Save(); for both of the prefs? or where would i put that in, and yes weirdly enough my highscore saves through sessions
Any time you call this function, it should save all the playerprefs that you set before, during this session.
Your answer
Follow this Question
Related Questions
Saving Data! PLEASE HELP 1 Answer
saving highscores 1 Answer
PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers
Saving and loading game with PlayerPrefs 1 Answer
Adding Value to Already Saved Values 1 Answer