- Home /
Creating score based on time with PlayerPrefs
Okay so im trying to make it so the players score is based on the time they survive. And I was trying to keep the highscore. I mushed 2 tutorials together and got this but its not working. I want to connect the score from the script to a UI text.
Mabey Ive got this all wrong pretty fresh to unity.
{ public Text score; public Text highScore;
void Start()
{
highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
}
public void HighScore()
{
int score = Time.deltaTime;
score.Text = score.ToString();
if (score > PlayerPrefs.GetInt("HighScore", 0))
{
PlayerPrefs.SetInt("HighScore", score);
highScore.text = score.ToString();
}
}
}
Answer by UnityCoach · Oct 16, 2018 at 11:43 PM
Hey, several things :
Time.deltaTime is how much since last update. You could use Time.timeSinceLevelLoad or Time.realtimeSinceStartup or simply Time.time.
You could add to the score, calling the method from Update().
score += Time.deltaTime
score
would need to be a float so that it doesn't round the value of Time.deltaTime (which most of the time, it less than 0.5)You could also save the time as formatted text, and parse it back, say if you want to count in minutes, seconds, etc..
Hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220612180439im_/https://answers.unity.com/themes/thub/images/avi.jpg)