[URGENT]How to make a Time based score, record score in next scene and replace high score?

I wanted to make a time based score with score recorded in final scene and when the score is higher than the previous score, the high score will replace with a new record. I’ve done the timer in the game, but i don’t know how to make the record the score and replace high score. Can anyone please help me? I am making game in Unity 2D. Thanks.

Hi there!

If you’ve got the timer already in the game, when the game is finished (by either winning or dying, depending on your game) record that timer’s time. Convert that recorded time into whatever kind of score representation you want, and then with that number, you will want to store it for a high score. It could be done with a simple if statement.

if (newScore > highscore) {
    // New high score!
    highscore = newScore;
}
else {
    // High score hasn't changed
}

I’m not sure if you’re wanting to save this high score only for the time that the game is open, or if you want it to remember that score even if you close the game though. Here’s a link showing kind of how you can use PlayerPrefs to save a high score. PlayerPrefs holds data stored locally on your device, so if you put your high score in there, it will remain there even after closing the game. If you don’t need it remembered and are okay with having the high score reset every time the game is closed, you can just hold the high score variable inside of a game manager script.

There are a few options, you could create a static variable. That variable will carry over to a new scene and stay the same. You can also use player prefs to save the score after the player has died/game has ended or you can also use dont destroy on load, meaning your script will remain the same while changing scenes… I would recommend reading up on one of these options and finding which one works best for you.