- Home /
How to add highscore to this script?
Hi there, i am trying for hours now but i can't find any help on how to add a highscore system to this specific concept. With this script my score goes up every second, and when the gameover scene is triggered, i basically want to take the achieved points and update my highscore text with it. Can anyone somehow walk me through? Thanks a lot.
public class ScorePerSec : MonoBehaviour
{
public Text scoreText;
public float score;
public float pointIPS;
void Start()
{
score = 0f;
pointIPS = 3;
}
void Update()
{
scoreText.text = (int)score + " Points";
score += pointIPS * Time.deltaTime;
}
}
Answer by I_Am_Err00r · Aug 01, 2019 at 07:18 PM
public class ScorePerSec : MonoBehaviour
{
public Text scoreText;
public float score;
public float pointIPS;
public float highScore;
void Start()
{
if(PlayerPrefs.GetInt("HighScore") != null)
{
highScore = PlayerPrefs.GetFloat("HighScore");
}
score = 0f;
pointIPS = 3;
}
void Update()
{
scoreText.text = (int)score + " Points";
score += pointIPS * Time.deltaTime;
if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
{
score = highScore;
PlayerPrefs.SetFloat("HighScore", highScore);
}
}
}
That isn't the most performant way to do this, but I don't know where your gameover screen chunk of code is, or if it's even part of this code, but I would move this block from Update to that section:
if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
{
score = highScore;
PlayerPrefs.SetFloat("HighScore", highScore);
}
Do that so it isn't constantly updating the highscore value when it doesn't need to and wasting resources.
FYI PlayerPrefs is a quick way to save data in Unity, that should work, don't have a compiler to test or double check for spelling errors.
public class ScorePerSec : $$anonymous$$onoBehaviour
{
public Text scoreText;
public float score;
public float pointIPS;
public float highScore;
void Start()
{
if(PlayerPrefs.GetFloat("HighScore") != null)
{
highScore = PlayerPrefs.GetFloat("HighScore");
}
score = 0f;
pointIPS = 3;
}
void Update()
{
scoreText.text = (int)score + " Points";
score += pointIPS * Time.deltaTime;
if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
}
}
}
I told you I don't have a compiler! I saw a couple issues, this should be right now
Thanks again, also had to replace the "null" with 0 because of float can't be converted to null or something error.
How can i now hook up my highscore text to this, so i can display the highscore as well?
Thanks for helping me out
Add a Text variable highScoreText, then at Start maybe do this:
void Start()
{
if(PlayerPrefs.GetFloat("HighScore") != 0)
{
highScore = PlayerPrefs.GetFloat("HighScore");
highScoreText.text = highScore;
}
score = 0f;
pointIPS = 3;
}
And then do something like this here:
if(PlayerPrefs.GetFloat("HighScore") == null || score > highScore)
{
highScore = score;
PlayerPrefs.SetFloat("HighScore", highScore);
highScoreText.text = highScore;
}
If you want it to constantly update, just add it Udate like you do with score, with a check that if the score exceeds the previous, then update the highScore.
Ah i figured it out already, thanks a lot, you helped me a great deal. :)
Your answer
Follow this Question
Related Questions
Unity Wiki Highscores Problem 1 Answer
Displaying Highscores Problem 0 Answers
Score not resetting 3 Answers
Saving score and applying highscore (C#) 2 Answers
I need some help with my high score.,I need help with my highsore. 1 Answer