- Home /
"Best Time"/high score help…
Hi again. Yet another question from me. (I'm learning!) I've been doing a lot of searching regarding this but have yet to find anything useful and as you probably know, I'm not a genius when it comes to scripting, so I'm asking here.
I'm making a game. (my Skyroads-like game I keep mentioning in my questions) The next thing I really need to do to make way for other things, is set up a best time/high score system. Throughout each stage there will be a timer running from the beginning to the end. It will be counting up of course. I want it to get the time at the end, check to see if it's the "best time" and save it. (I guess in PlayerPrefs) I want each individual stage/level to have its own best time of course. Then, at the level select screen, there will be a "Best Time" below each level and it will display the "best time" for that level/stage.
I already have a timer counting up. I haven't told it when to stop yet, since I don't actually have a complete level that "starts" and "ends" but I'll get to that.
I'd just like a place to start. You don't need to write any code out for me if you don't want to. I just want some direction. I've looked into PlayerPrefs a bit, looked at other high score questions, but they're all about a high score table with a name and all that. I just want ONE "high score" (best time taken from the Timer's variable at the end of the level I guess) to be saved and displayed on the level select screen at a certain point on the screen. (Of course) Plenty of other things I might do with it, but I can figure all that out myself. For now I just need some help figuring this out.
……
TL;DR, I want to do this:
Have a timer counting up (on any given stage, whatever, this is already done, unless I need to re-do it to make up for the method I'll use to save it and all)
Save the timer's "time" when a certain thing happens (the level is "completed", I guess a boolean or something, whatever, not important)
Check to see if it's the best time (check the previous best time, if there is none, then it should be 0 or something and it will know there is no best time, so it is automatically the best time)
Display the best time somewhere. I'm not asking for you to write it all out for me, just pointing me in the right direction with a few tips will do.
Sorry for such a long post, I'm somewhat of a perfectionist. Thanks for any help in advance. :)
You are so close to doing it all by yourself its not even funny ;)
He's right you basically have it. One suggestion though is to use math.infinity ins$$anonymous$$d of 0 on your best time check, that way you only need to do the < test and not an extra test for 0 first.
You can use $$anonymous$$athf.$$anonymous$$in to evaluate all your scores and return the $$anonymous$$imum. :P
Answer by by0log1c · Apr 11, 2012 at 06:04 AM
Well this is UnityANSWERs, so I'll take a minute to write a pseudo-code, assuming you use JS. I won't comment it as its pretty self-explanatory.
static function UpdateScore(currentScore:int)
{
var highestScore:int = PlayerPrefs.GetInt(Application.loadedLevelName+"_highscore");
if(currentScore <= highestScore)
return;
PlayerPrefs.SetInt(Application.loadedLevelName+"_highscore",currentScore);
Debug.Log("Congratulation! You just beaten the previous high score of: "+highestScore+" with a score of: "+currentScore+" in the level: "+Application.loadedLevelName+"!!!");
}
This looks a lot like what I was expecting, and what I need, but it just doesn't really make sense to me. I see this type of stuff in a lot of PlayerPrefs examples.
$$anonymous$$ind adding maybe a few comments (in the script) or just a little clarification on what exactly each thing does? Thanks so much for taking the time, though. :) And I figured I was really close, heh. I'll get this. :D
Well, basically... The first line of the function retrieves the highest score saved as [NameOfTheCurrentScene]_highscore. The second statement compares both scores, if the previous one was better, the function exits(does not complete). The third statement update the saved high score under [NameOfTheCurrentScene]_highscore. The last line just prints a note to the console for information purpose.
Thanks so much! I've got it working. But I am having trouble displaying the "highestScore" (best time) later, such as within that level and especially in another scene (level select screen)
Any help on that using your script? I've seen other methods but they aren't working.
EDIT: Never $$anonymous$$d all that, I've got this script working almost perfectly. Converted it to a time format and made it display where I needed it to, tweaked it a little, etc. And you are right about me almost having it before. Very simple once you know how it all works.
Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613070154im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Timer highscore trouble 1 Answer
using player time as highscore 1 Answer
One PlayerPref not saving 1 Answer
HighScore - Timer 1 Answer
Is there a decent tutorial for a local high score table (android)? 1 Answer