# Coroutine Infinite Loop Help!

So pretty much I want to make a timer that will be used to trigger something. I’m in the middle of setting up an infinite loop for the timer. My code is as follows:

``````// Sets changeable times. If the original values change then its hard to re-use them.
float triggerTimer = defaultTime;
float variationCounter = variationTime;

// true if the corrosponding properties will go on forever.
bool timedInfiniteIntervals = false;
bool timedInfiniteLoops = false;

// Sets the infinite properties
if (intervalAmount == -1)
timedInfiniteIntervals = true;
if (loopAmount == -1)
timedInfiniteLoops = true;

if (doLoops == true) {

// This runs while there are still loops left.
while (loopAmount > 0 || infiniteLoops == true) {

while (intervalAmount > 0 || infiniteInteverals == true) {
yield return new WaitForSeconds (triggerTimer);
Debug.Log ("Trigger Activated");

// This resets the timer and adds on the extra variation to the time
triggerTimer = defaultTime + variationCounter;

variationCounter += variationTime;

IntevralAmount -= 1;
}

Debug.Log ("Loop " + loopAmount + " was Successful");
triggerTimer = defaultTime;
loopAmount -= 1;
}

} else {
// This path doesn't incorperate loops. The code is repeated for easier testing.
while (intervalAmount > 0 || infiniteInteverals == true) {
yield return new WaitForSeconds (triggerTimer);
Debug.Log ("Trigger Activated");

// This resets the timer and adds on the extra variation to the time
triggerTimer = defaultTime + variationCounter;

variationCounter += variationTime;

IntevralAmount -= 1;
}
}
``````

So pretty much I have two loops, one inside the other. The point of the second loop is so I can add variation to the timing. For example…

lets say that the following is set:
intervalAmount: 2
defaultTimer: 1s
variationTimer: 5s
loopAmount: 3

the timer would go like this (each number represents the new time after an activation)
1, 6 (that’s one loop of 2 intervals increasing by 5) 1, 6 (another loop) 1, 6 (third loop)

Another example…

lets say that the following is set:
intervalAmount: 4
defaultTimer: 1s
variationTimer: 5s
loopAmount: 3

then the timer would go like this:
1, 6, 11, 16 (that’s one loop of 4 intervals increasing at 5) 1, 6, 11, 16 (another loop) 1, 6, 11, 16 (third loop)

So now that you understand how it’s meant to work. My problem:
If I set the intervalAmount to infinite. it will works.
But if I set the loopAmount to infinite, it doesn’t and unity crashes.

I have a feeling it’s because there are 2 while loops inside the infinite looping system, because thats the only difference between the two sections.

FoodLover195

Just so I don’t misunderstand Inverval amount = inner loop and loopAmount = outer loop.

Your code won’t work even with non infinte. Because your outer loop doesn’t reset the interval amount after it gets done on time. So in your original example it will do this:
1,6 then loop amount will go 2 and 3 skipping the inner loop for the 2nd and 3rd time because interval amount is still set to -1.

This is also why the infinite loop on the outer loop fails. Because it does the inner loops 1 time (hitting all those yield returns) , but after the inner loop fires on time, it stays in the outer loop infinitely without any yield returns.

You need to create a variable to store the original Interval amount, and reset intervalAmount back to its original value at the end of every outer loop

2 Likes

If Unity hangs, that means that your yield return new WaitForSeconds (triggerTimer) never executes. (Or triggerTimer is zero, but that might still be enough to prevent Unity from hanging, I don’t know for sure.)

The logical reason that could happen is if your outer loop condition is always true, and your inner loop condition is always false. Because in that case, no yield ever gets executed. So I’d recommend either figuring out when and why your inner while loop’s condition gets stuck permanently as false, or add another yield inside the outer loop, but not inside the inner loop, so that even if the inner loop never executes, at least one yield per outer loop iteration occurs.

Of Course! Thanks so much. I was smart enough to realise that i needed to reset the timer, but apparently I forgot about the resetting the inner loop.