Timer (save/display/milliseconds)

Hello,

I am trying to make a timer which does 3 things…

  • goes up in minutes/seconds/milliseconds(00:00:00).
  • save time
  • display best time

I don’t know on how i go about doing this, this is closest I got…
Any help or advice will be greatly appreciated. Thanks

    public Text timerText, timerTextFin, bestTimeText;
    private float startTime;

    private bool finished = false;

    float endTimeFloat, bestTimeFloat;

    public PlayerControl thePlayer;
    public GameManager theGameManager;

    public GameObject theTime, finishedTime, outOfTime;
       
    void Update(){
//TIMER
        float t = Time.time - startTime;
//STOPS TIMER IF PLAYER REACHES FINISH POINT
        if (finished) {
            return;
            t = bestTimeFloat;
            PlayerPrefs.SetFloat ("bestTimeFloat", bestTimeFloat);

            if (t < bestTimeFloat) {
                timerText = bestTimeText;
                PlayerPrefs.GetFloat ("bestTimeFloat");
                PlayerPrefs.Save ();
            }
        }
//TIMER WHICH HAS (00:00.00)
        string minutes = ((int)t / 60).ToString ();
        string seconds = (t % 60).ToString ("f2");
        timerText.text = minutes + ":" + seconds;
        timerTextFin.text = minutes + ":" + seconds;
        bestTimeText.text = minutes + ":" + seconds;

//IF TIME REACHES 5mins STOPS GAMEPLAY
        if (t >= 300.00) {
            finished = true;
            theTime.SetActive (false);
            outOfTime.SetActive (true);
        }
    }
       
    void OnTriggerEnter(Collider other){
        if (other.gameObject.tag == "Player") {
            finished = true;
            thePlayer.enabled = false;
            theTime.SetActive (false);
            finishedTime.SetActive (true);
        }
    }
}

ues the TimeSpan class it has a FromSeconds static method that will take seconds and create a timespan from it. Than you can query its properties to get the values you want

TimeSpan t = TimeSpan.FromSeconds( secs );

string answer = string.Format("{0:smile:2}h:{1:smile:2}m:{2:smile:2}s:{3:smile:3}ms",
                t.Hours,
                t.Minutes,
                t.Seconds,
                t.Milliseconds);