In the code below I am resetting a timer after a duration maxiTime within the update method. I want to delay the timer before it begins to run for a certain duration. I believe this could be done with a coroutine, but I am using StopAllCoroutines, so I need to do this within the update method. How can I get the delay only before the first run?
timeElapsed +=1;
if(timeElapsed >= maxiTime) {
timeElapsed = 0;
Debug.Log ("Thank You!");
}
So you need it delayed before it hits the reset? I suppose you could just have timeElapsed start at a negative value so it takes it longer to get above maxiTime(for the first time through), but still just reset it to 0 like you have it.
Also, you could always have a different script that runs the timer coroutine so it doesn’t get stopped by stopallcoroutines(I believe this only stop the coroutines on the same instance it is called on, but I haven’t used it before)
1 Like
Put the code in it’s own MonoBehaviour and disable it after you’ve run what you need. Unless you want it to run over and over again and just delay the first time in which case:
bool go;
IEnumerator Start()
{
go = false;
yield return new WaitForSeconds(delayTime);
go = true;
}
void Update()
{
if (!go) return;
timeElapsed += Time.deltaTime;
if (timeElapsed >= maxTime)
{
timeElapsed = 0;
Debug.Log("foo");
}
}