Can you help me do a highscore please? I've got this problem..
if (isdead == false) { score += Time.deltaTime * 25;
scoreText.text = ((int)score).ToString();
if (scoreText > PlayerPrefs.GetInt("Highscore"))
{
PlayerPrefs.SetInt("Highscore", scoreText);
}
} // I tried using score instead of scoreText in player prefs btw
Answer by dval · Jul 03, 2017 at 05:28 PM
You posted some bad code, and said you have a problem. It helps if you post the actual error from the the Unity console.
you're trying to store the Text GameObject, not it's text value, this should be scoreText.text
you've cast score as a string and trying to store value in int, you should store (int) score.
//updated if (isdead == false) { score += Time.deltaTime * 25; scoreText.text = ((int)score).ToString(); if (score > PlayerPrefs.GetInt("Highscore")) { PlayerPrefs.SetInt("Highscore", (int)score)); } }
Additionally, I hope this is meant only to be updated after the character reaches end of level. Else, once your character reaches a high score, you start updating PlayerPrefs on each frame. This would kill performance and cause much needless activity.
Edit: I fixed the "operand > can't be used..." error. That was bad reading on my part, and maybe the root of the error.
if (scoreText > PlayerPrefs.GetInt("Highscore")) // bad
is trying to compare a GameObject is greater than an Integer. should have been:
if (score > PlayerPrefs.GetInt("Highscore")) // good
Which compares the int 'score' with the int 'Highscore'. This is something the computer can do.
That gives me an error in if (scoreText > PlayerPrefs.GetInt("Highscore"))
the error is operator > cannot be applied to operants of Text and Int
take it easy on me man im just a newbie in unity.. im starting, thats it.. and thanks btw :)
@DoubleD17 Sorry, I was having a grump day... I updated the example, was able to compare highscore with it. Hopefully the fixed example works for you.
Your answer
Follow this Question
Related Questions
Saving Highscore with PlayerPrefs not working 0 Answers
mathf.abs(0) is higher than 90, why? 1 Answer
Help with rotation please 2 Answers
How to use the button to call for a method 0 Answers
If statements with AudioListener C# ? 0 Answers