- Home /
Making a scoreboard with a timer
Hi there, Im making a race game, I have a timer ( Time.DeltaTime ) running until I reach the finish line ( with a script endRace on the line ). Then the timer stops.
I simply want to load a new scene that keeps the best time ever. I dont know where to put the player.prefs thing ( on my timer script ? ... ).
I know that can look easy for some of you but for me its kind of complicated.
THX
Answer by nanomo · Apr 02, 2013 at 02:27 PM
Use a timer like this, then figure out how to stop it and share players time, this can be the baseline:
using System; using UnityEngine;
[RequireComponent(typeof(UILabel))] public class TimeDisplay : MonoBehaviour {
private UILabel label; protected void Start() { label = GetComponent(); }
protected void Update() { TimeSpan ts = TimeSpan.FromSeconds(GameManager.Instance.timer); label.text = ts.Minutes.ToString("00") +":" + ts.Seconds.ToString("00"); }
}
Answer by zBlanco · Apr 02, 2013 at 02:29 PM
Well, using PlayerPrefs, first create a variable called raceTime or something of that nature. Next, find the code where you stop the timer. Then use a check to see if the raceTime of that race is lower than the "BestTime" stored in a PlayerPref.
So for an example:
var raceTime: float;
.
.
. //raceTime is the time it took to race, stored after timer stops
. //Code where timer stops
if(raceTime<PlayerPrefs.GetFloat("bestTime"){
PlayerPrefs.SetFloat("bestTime", raceTime);
}
To call back the best time, all you would have to do is use PlayerPrefs.GetFloat("bestTime");