0
\$\begingroup\$

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!");
    }
\$\endgroup\$
  • \$\begingroup\$ It is not exactly elegant but you can use a second timer if you insist the code to be strictly in Update. Or you can try to use Invoke. Or if you can instead stop coroutines separately and use coroutine for the delay. \$\endgroup\$ – Nikaas Jul 21 '17 at 18:38
  • 1
    \$\begingroup\$ Start() { timeElapsed = -1; } ? \$\endgroup\$ – Draco18s no longer trusts SE Jul 21 '17 at 19:22
  • \$\begingroup\$ @Draco18s Thanks, starting from a negative value solved the issue \$\endgroup\$ – SuperHyperMegaSomething Jul 21 '17 at 20:28
  • \$\begingroup\$ Hmm.. the root problem might be your use of StopAllCoroutines in the first place. If you're only able to use coroutines that all stop together when any one of them needs to be stopped, that's going to be very limiting on what you can do with them. Depending on your situation, you may be able to cache a reference to the coroutines you need to stop, and stop them individually with StopCoroutine(_coroutineReference) without interfering with your other coroutine behaviours. If you want help with that, it could be worth asking as a separate question. \$\endgroup\$ – DMGregory Jul 22 '17 at 0:55
0
\$\begingroup\$

For something like this you can just start the timer at a negative value. It's easy, simple, and gets the job done.

Start() {
    timeElapsed = -1;
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.