Every time my game runs the high score is not saved and overrun every time the game ends no matter what the number is. If i finish with a score of 5 and then die at 2 it saves to 2 and not 5 Why is it not working ?
public Text scoreText;
public Text highscoreText;
public float scoreCount;
public float hiScoreCount;
public float pointsPerSecond;
public bool scoreIncreassing = true;
void Start()
{
PlayerPrefs.SetFloat("highscore", hiScoreCount); //save
}
void Update()
{
if (scoreIncreassing)
{
scoreCount += pointsPerSecond * Time.deltaTime;
}
if (scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
}
scoreText.text = "Score: " + Mathf.Round (scoreCount);
float highscore = PlayerPrefs.GetFloat("highscore"); //read
highscoreText.text = "Highscore: " + Mathf.Round (hiScoreCount);
}
Don’t make multiple threads for your same issue. Original Thread
A few problems with this code:
You are still not initilizing any of your variables at the top, they all need to have their starting values entered. ie: public float scoreCount = 0.0f;
You are only saving the highscore in Start(). Start() is called when the scene is initialised. You need to have it in Update so that it updates every frame when the condition to update it is met (ie when the highscore is increased).
You’ve put the Read for the PlayerPrefs in the Update, no reason for that. You only read it when you want to retrieve the high score, ie in Start() is probably where you want to put it.
It seems like you are very new to coding and you are not grasping the understanding even with the words save and read. I recommend watching and building things using the Unity Tutorials on their website or following beginners tutorials on YouTube.