Turn off InvokeRepeating

I have an InvokeRepeating script

InvokeRepeating(“Shoot”, shootInterval, shootInterval);

It works fine - making my character continually shoot, but every 10 seconds, I want him to stop shooting and run around the field (i’ll use an animation for that).

Then he will go back to his original position and continue shooting at intervals

The question is: is there a way to turn off InvokeRepeating, and is the also a way to pause it, and then continue it?

No way to pause it unless you use a boolean switch (checking a boolean value in the function it calls)

CancelInvoke () to cancel all invoked methods in the MonoBehaviour
or
CancelInvoke (methodName : string) to cancel specific methods.

5 Likes

You want to use coroutines for this sort of thing, rather than InvokeRepeating.

–Eric

1 Like

Why?

Because it’s way easier to control.

–Eric

But the scope of Invoke is limited to the class - adding CancelInvoke() to stop the repeat is pretty simple isnt it?

Not really; the program flow becomes non-obvious and it seems like it would be easy to introduce bugs.

–Eric