- Home /
Formats of High Score
Hello everyone,
I have a running timer in my app and when user collides with an object, the timer saves itself as high score.
Everything is working fine for now.
However, when i pass my high score, the high score text shows itself as Number.number When i cannot pass the latest highscore, it shows itself as only Number.
I do not want it to show as number.number everytime user breaks his record. Only with number.
I would be grateful if you could help me. I have played with ToString options but could not make it happen.
BTW, this script works alright for timer and high score. I hope it would be useful for anyone.
public Text timerText;
public Text highscore;
private float startTime;
void Start()
{
startTime = Time.timeSinceLevelLoad;
highscore.text = "High Score : " + PlayerPrefs.GetFloat("HighScore").ToString("0");
}
void Update()
{
float t = Time.timeSinceLevelLoad;
if (t > PlayerPrefs.GetFloat("HighScore", 0))
{
PlayerPrefs.SetFloat("HighScore", t);
highscore.text = "High Score: " + t;
}
startTime += Time.timeSinceLevelLoad - startTime;
string seconds = (startTime % 60).ToString("0");
timerText.text = seconds;
}
Answer by Priyanka-Rajwanshi · Jul 04, 2018 at 05:26 PM
@androidkafkaskafkas If you do not need the decimal value, convert the high score into int and save.
int highScoreVal = Mathf.RoundToInt(t);
PlayerPrefs.SetInt("HighScore", highScoreVal);
may I ask into which part i shall implement this code ?
thank you.
According to your code, you should do it in Update in the if condition. If the high score is seen only on game over and not while gameplay, then you should put this if condition and the above code snippet at the place where you check that game is over. ($$anonymous$$ake sure to use SetInt and GetInt ins$$anonymous$$d of SetFloat and GetFloat.)