N
N
Netrox562022-01-07 11:07:22
2D
Netrox56, 2022-01-07 11:07:22

Updating the best score (highscore) occurs every time Unity, How can I decide?

Guys, my highscore works fine, only now it is updated every other time. For example: highscore is 5 and in the game I score 10 points and lose then on the highscore inscription it still writes 5, but after I clicked a new game (restart) and lost without breaking the record (highscore), this time the highscore is updated and writes 10. How can I solve this?

public Text highScoreText;
public Text scoreText;
public Text finalScoreText;

private int scoreNum;
private int easyHighScoreNum;
private int mediumHighScoreNum;
private int hardHighScoreNum;

private void Start()
{
scoreNum = 0;
scoreText.text = scoreNum.ToString();
finalScoreText.text = scoreNum.ToString();

if (PlayerPrefs.HasKey("easy"))
{
easyHighScoreNum = PlayerPrefs.GetInt("easyHighScore", 0);
}
else if (PlayerPrefs.HasKey("medium"))
{
mediumHighScoreNum = PlayerPrefs.GetInt("mediumHighScore", 0);
}
else if (PlayerPrefs.HasKey("hard"))
{
hardHighScoreNum = PlayerPrefs.GetInt("hardHighScore", 0);
}
}

private void Update()
{
if (PlayerPrefs.HasKey("easy"
highScoreText.text = easyHighScoreNum.ToString();
}
else if (PlayerPrefs.HasKey("medium"))
{
highScoreText.text = mediumHighScoreNum.ToString();
}
else if (PlayerPrefs.HasKey("hard"))
{
highScoreText.text = hardHighScoreNum.ToString();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("PresentObj"))
{
scoreNum++;
scoreText.text = scoreNum.ToString();
finalScoreText.text = scoreNum.ToString();

if (PlayerPrefs.HasKey("easy"))
{
if (easyHighScoreNum < scoreNum)
{
PlayerPrefs.SetInt("easyHighScore", scoreNum);
}
}
else if (PlayerPrefs.HasKey("medium"))
{
if (mediumHighScoreNum < scoreNum)
{
PlayerPrefs.SetInt("mediumHighScore", scoreNum);
}
}
else if (PlayerPrefs.HasKey("hard"))
{
if (hardHighScoreNum <
PlayerPrefs.SetInt("hardHighScore", scoreNum);
}
}

}
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-01-07
@freeExec

In your sheet of text, no code, no record update.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question