- Home /
Timer running down too quickly
I have two timers running, they both trigger one another so when one reaches just below 0 it triggers the other to start until it reaches just below 0. The thing is the timers are running down extremely quick, although they are set at 30, they run down in a bout 5 seconds. Any ideas? I've tried setting the Time.timeScale = 1 but that changes nothing.
public static float timerOff = 30;
public static float timerOn = 0;
private bool isTiming = true;
// Use this for initialization
void Start ()
{
Time.timeScale = 1.0f;
}
// Update is called once per frame
void Update ()
{
if (isTiming == true)
{
timerOff = timerOff - 1 * Time.deltaTime;
if(timerOff < 0)
{
isTiming = false;
timerOn = 30;
}
}
if (isTiming == false)
{
timerOn = timerOn - 1 * Time.deltaTime;
if (timerOn < 0)
{
isTiming = true;
timerOff = 30;
}
}
print(timerOff + "off");
print(timerOn + "on");
}
That script works fine for me, it counts down in exactly 30 seconds. $$anonymous$$aybe you can try debugging out what Time.deltaTime is for you? Do you have anything else in your scene? Try using the script alone in a new project and see if that works.
Only using it on one object, it was working fine the first few times I tried it, after changing some other things it started to do this. I changed the other things back however the time is still going at this speed. By the looks of it the script is fine, it's just something glitchy, just wanted to know if it was the script or not causing the problem.
In the Start of your script add:
Debug.Log("D'oh!!");
just to make sure you would not have it twice
You could also make sure you only have one:
static ClassName _instance = null;
void Awake(){
if(_instance == null)_instance = this;
else Destroy(this);
}
Answer by Melih Şimşek · Dec 17, 2013 at 03:42 PM
I suggest you to use
timerOff -= Time.deltaTime;
Instead
timerOff = timerOff - 1 * Time.deltaTime;
Tried that already, tried all different ways of using Time.deltaTime I can think of.
Your answer
Follow this Question
Related Questions
Timer help please 1 Answer
How would you make your player freeze for a certain amount of time? 2 Answers
A node in a childnode? 1 Answer