- Home /
Playerpref is not saving
Not sure what is causing this issue but here is my code using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public Text scoreText;
public Text highScoreText;
public float scoreCount;
public float highScoreCount;
public float pointsPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start () {
if (PlayerPrefs.HasKey("highScore"))
{
highScoreCount = PlayerPrefs.GetFloat("highScore");
}
}
// Update is called once per frame
void Update() {
if (scoreIncreasing) {
scoreCount += pointsPerSecond * Time.deltaTime;
}
if (scoreCount > highScoreCount)
{
highScoreCount = scoreCount;
PlayerPrefs.SetFloat("hightScore", highScoreCount);
}
scoreText.text = "Score: " + Mathf.Round(scoreCount);
highScoreText.text = "HighScore: " + Mathf.Round(highScoreCount);
}
}
How to do you get the score :) . Cause maybe its saving empty
Answer by OneCept-Games · Jan 05, 2018 at 12:52 PM
You have spelling mistake. You set hightScore, but retrieve highScore. remove the "t"
THAN$$anonymous$$ YOU! I never noticed this.
a good practice for avoiding token inconsistencies like this is to make a constant with the token:
static readonly string kHighScore = "highScore";
...
PlayerPrefs.SetFloat(kHighScore, foo);
...
foo = PlayerPrefs.GetFloat(kHighScore);
that way if you have a type in spelling the token variable you get a compile error.
Your answer
Follow this Question
Related Questions
Grabbing PlayerPref float and checking it's value? 2 Answers
Unity problem when i convert playerprefs save to the standard file io save 0 Answers
How to Save Object (Button) information on game restarting? please check code. 2 Answers
How can I save my game data 1 Answer
Using XML files and playerprefs together 0 Answers