Timer stop and check

I’m quite new to scripting and need a timer for my racing game. I have the visual timer on screen and I want to make it stop on trigger, how can i make it stop? Can I make it show on the next scene? And how would i go about making a system that checks if you’ve got to the finish in time? For example, to get to the next level, you need to get there under 02:45:00, how can I check that the players time is less than that and if it is over that time load a different scene? I need a lot of help, don’t know much about C#. If you can, send scripts to help and explain them, thanks!

using System.Collections;
using UnityEngine.UI;
using UnityEngine;

public class Timer : MonoBehaviour
{

    public Text timerText;
    private float startTime;

    void Start () {
        startTime = Time.time;

    }
  
    // Update is called once per frame
    void Update () {

        float t = Time.time - startTime;

        string minutes = ((int)t / 60).ToString("00");
        string seconds = (t % 60).ToString("00");
        string miliseconds = ((int)(t * 100f) % 100).ToString("00");

        timerText.text = minutes + ":" + seconds + ":" + miliseconds;
    }

}

It might be slight overkill but my Datasacks system can be used for both keeping track of the time elapsed and displaying it, and of course checking it against other values.

In fact, the micro game inside the Datasacks package contains a countdown timer so you can see how its used, as well as lots of other features.

Datasacks is presently hosted at these locations:

https://bitbucket.org/kurtdekker/datasacks

Thanks, quite helpful but I’m not sure I understand how to use it. I’m really new to this, simple methods and explanations please

Firstly, do you have a trigger in the scene and an OnTriggerEnter() override function? you need this function to access collision data. In this function you check if the object is correct and than you stop the timer.

To make it show on the next scene you need something like DontDestroyOnLoad() which makes it so that the object can persist between scenes. I recommend using one object with a script that keeps track of all the race times and maybe the top scores if you’d like.

To check whether or not the player made it in time you can simply decrease your target time with the players time, if that is bigger than 0 that means he was in time. use an if statement to load the correct scene using the scenemanager.

1 Like

thank you this helped SO much!

Not a problem, glad I could help.