Resetting wait for seconds

My code has a bunch of waitForSeconds functions that control the spawning of objects. My problem is when I use a restart function the waitForseconds function is still running even if I pause that piece of code. So when things start repawning it’s a lot more than it should be… I’m not sure if it’s possible to reset any of these waitForSeconds timers ?

If you want to stop all coroutines currently running, you can try StopAllCoroutines() - I’ve not used this before, but I suppose this function can kill all of them.

But if your problem is new coroutines being started while the older ones have not finished yet, the best solution (always recommended with coroutines) is to have a boolean flag for each process: 1- only start a new cycle if the flag is false; 2- set flag to true at the beginning; 3- reset flag to false at the end. Something like this:

var spawnFlag: boolean = false; // boolean flag for this coroutine

function spawn() { // only starts a new cycle when the last one ended
    if (gameIsOver == false && spawnFlag == false){
        spawnFlag = true; // new cycle started...
        yield WaitForSeconds(Random.Range(40,60));
        spawnFlag = false; // cycle ended