- Home /
JavaScript Timer/Highscore Problem
I tried to make a script which would let me press a button to see the current time that has passed since the level started but it just prints 0 when i press the button. I have pressed I to stop the recording and tried in many different ways. Im a bit new to java so please be patient with me. Thanks for any help i might get.
here is my codes:
static var record : boolean;
static var currentTime : float;
static var bestTime : float;
function Start () {
// Reset current time
currentTime = 0.0;
// Start record
StartRecord();
}
function Update () {
// Keep adding to currentTime if record is true
if (record)
currentTime += 1*Time.deltaTime;
// If Player press R then run Respawn function
if (Input.GetKeyDown(KeyCode.R))
Respawn();
// If Player press I then stop recording time
if (Input.GetKeyDown(KeyCode.I))
StopRecord(true);
}
function Respawn () {
if (!Application.isLoadingLevel)
Application.LoadLevel(Application.loadedLevel);
}
static function StartRecord () {
record = true;
}
static function StopRecord (checkBestTime : boolean) {
record = false;
if (checkBestTime && currentTime<bestTime) {
bestTime = currentTime;
PlayerPrefs.SetFloat("Score", bestTime);
}
}
And this:
function OnGUI () {
if (GUI.Button (Rect (0,0,200,100), "Click for highscore")) {
print (PlayerPrefs.GetFloat("Score"));
}
}
where is "checkBestTime" defined, what is it's value.
Can you please tell us what you did. You asked a question and you found the solution. UA is here to help everyone. You are not helping anyone if you are not giving us your solution.
Answer by Crazydadz · May 12, 2013 at 08:09 PM
your default besttime value is 0. So you will never beat your best time. You should first check in your StopRecord function if bestTime is 0 then set your bestTime to your currentTime. After that, this should work.
static function StopRecord (checkBestTime : boolean) {
record = false;
if (checkBestTime && (currentTime<bestTime || bestTime == 0)) {
bestTime = currentTime;
PlayerPrefs.SetFloat("Score", bestTime);
}
}
Your answer

Follow this Question
Related Questions
Elevator script problem (Java) 1 Answer
Timer Between Labels 2 Answers
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
Talking Code 2 Answers