Example, the code above enters the while loop, runs the code once, then waits 5 seconds before it loops.
In your code, it runs the if statement, starts the coroutine which then hits the 5 seconds mark, returns to the Update and runs the rest of the code in Update before looping again to run Update again. It does not pause your code in Update. There are also ways to have it skip code in Update using your own timer and some prefer that, but otherwise, just run all the Update code in your coroutine with the yield in there and it will be fine.
Part of the issue is you are calling InvokeRepeating from Update. What is happening is it calls it every single frame which means it continues to build up calls over and over. You now have 100s or 1000s of InvokeRepeating calls building up, so it does technically wait 5 seconds, but if you got 60 fps for example, you now have 60 calls per second, all in a nice queue. And it just keeps stacking.
And I wanted to see your coroutine call because now after seeing this, I suspect you had the same issue with your coroutine. You probably called StartCoroutine in update over and over and over again, creating several looping coroutines.
You need to do the calls once in Start and only call it again if you stop it for some reason but need to restart it.
So the first one will work fine, but after all the other calls just execute in a line.
question: if I call the coroutine in the start method with a while loop inside the coroutine like you sais, when I turn the bool variable on and off, do I need to call the coroutine again?
Yes, if you set the bool to false, the coroutine would exit the while loop and end, thus you’d have to call it to restart it. Or leave it looping in a condition where it doesn’t run any code for a bit.
I’m also not a fan of InvokeRepeating due to the lack of compile time checking, but glad you got your code working.
As you can see, InvokeRepeating takes a string. Because it’s just a string, when you return to Unity and it recompiles your code, it doesn’t check if that method actually exist. So right now you are safe, but what happens if you change the method name. Then you return to the game, hit play and suddenly it’s not working right and maybe you get lucky and figure it out, but maybe you struggle for hours getting stuck. Or, if you ever work with another developer who changes the name and suddenly they don’t know what is going on.
Now you might think you’ll name do either of those, but it does happen and I’ve seen that mistake several times with newer programmers at my work.
If you use the generic version of the coroutine when you start the coroutine (don’t use the string version as this runs into the same issues), it will act just like you trying to call a method and throw an error as soon as you return to Unity if you didn’t see the error pop up in your script editor software that the method doesn’t exist.
my problem with the coroutine is once it turned of I can figure a way to restart it when I need it without puting it in update and that’s a no go since I come back to my early problem.
for now I think i’ll stick to invok repeating in the start method