Highscore Not Updating Upon Death
Okay, I wrote this last night, and it worked then. It no longer does today. The only things I changed were adding a Quit Game button upon death. Code:
public class SetHighScore : MonoBehaviour {
public Text highScore;
public int HighScore;
void Start ()
{
FindObjectOfType<Score>().HighScore(); // Calls function which is pasted below.
HighScore = FindObjectOfType<Score>().highScore; //Calls integer from the HighScore function.
highScore.text = "HighScore: " + HighScore.ToString(); // Sets HighScore text to integer that was just updated then called. I think.
}
}
Everything is good so far, right? Here is the function called in the first line
public void HighScore ()
{
if (score > PlayerPrefs.GetInt("HighScore", 0))
{
PlayerPrefs.SetInt("HighScore", score); // Sets highscore.
highScore = PlayerPrefs.GetInt("HighScore"); //sets integer to new high score.
}
}
I know that the Start funtion being used is not the issue, as the GameObject that this script rests on is only enabled upon death. Sorry if this post is hard to follow. If you need any more info, just ask ^.^
Answer by Rhombuster · Apr 07, 2017 at 12:40 AM
It appears that your integer is called HighScore, not highScore. So I think your function at the button should say HighScore = PlayerPrefs.GetInt("HighScore");.
The way you named everything is making the code confusing. There are no hard and fast rules, but try to make your function names more descriptive. You could call it CheckHighscore() or even UpdateHighscore(). I'd also consider changing the variable names of highScore and HighScore to make them more distinguishable.
Such a small oversight can cause such a big issue. Thank you! I do agree, the code is very confusing. I took a few $$anonymous$$utes to define everything to much greater lengths in the names. Thanks for the help!