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.
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
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
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
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.