Start timer on new scene

I’m making a basic stop the clock game i have a start menu and when you click start i want the game to flip to the new scene where the timer will start but when I click start the timer shows up having started already. Basically i want the timer to wait till after I click start but in its current state it seems to be starting once i start the game. Any ideas?
I attached all my scripts. i’m sure the problem is obvious i just cant seem to find it.

3179989–242469–LevelManager.cs (440 Bytes)
3179989–242470–StopClock.cs (304 Bytes)
3179989–242471–Timer.cs (997 Bytes)

Here on the forums we would rather not have to download your files. Please Use Code Tags and paste them here.

I agree - better to post code on the forums. However, even without viewing your code, I’d suggest that you move the timer script to the new scene… that way it’ll start up when that scene loads and not the first scene :slight_smile:

It is in the second scene but still seems to start in the first scene. And sorry for any bad posts this is my first post

Well, could you post your code on the forums?
Look at this post for how-to: Using code tags properly - Unity Engine - Unity Discussions

If you’re using the game “running time” that’s the only thing I can think of, if the timer is running in the second scene that it might be affected. Otherwise, it shouldn’t start until its script is first run (which would only happen in the scene where it is)…

With the code here, maybe someone can offer some insight / suggestion(s) for you :slight_smile:

Heres my current code. sorry for the delay and any help will be appreciated. i do think you are right that my code is running during the games run time so it starts when the game starts but i cant figure out how to fix it.

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

public class Timer : MonoBehaviour {

    public Text timerText;
    float startTime;
    bool finished = false;
    string seconds;
    float t;

    // Use this for initialization
    void Start () {
        startTime = Time.time;
    }
   
    // Update is called once per frame
    void Update () {
        if (finished)
            return;

        t = Time.time + startTime;

        seconds = (t % 60).ToString("f2");

        timerText.text = seconds;
    }

    public void Finish()
    {
        if (t == 5.00f)
        {
            finished = true;
            timerText.color = Color.green;
        }
        else if (t > 5.00f)
        {
            finished = true;
            timerText.color = Color.red;
        }
        else if(t < 5.00f)
        {
            finished = true;
            timerText.color = Color.red;
        }
    }
}

Time.time is the seconds since the game has begun.
If you are looking to count for 5 seconds, it would suit you to just use a float variable that you can increment by time.deltaTime. As in, begin at zero when this script loads :slight_smile:

Thanks a lot I changed my startTime to = 0f above start and changed t to t = time.deltaTime and it’s working now

The original script would work if the + on line 24 was changed to a -. Using Time.time is actually a good idea if you have multiple long running timers, as you avoid accumulating floating point errors.