I’m new to coroutines and I don’t really know what all is possible.
My situation now is, that I have two bools. If bool A is false and bool B is true I wan’t to start a coroutine X. If than these bools change there value in any way I want to stop and reset X and restart it again if A is false and B is true. How would I do that, I tried some stuff but it never does wat I want it to.
And if I write: if(A == false && B == true) startCoroutine(X); doesn’t X get started every single frame where the condition is met? Is that harmful in any way?
If that is in Update, then yes it will start the coroutine every frame while it is true.
That really depends on what your coroutine does and if you designed it/expect it to be running multiple times concurrently.
In most cases, you probably don’t want to do that. What you actually want is not to start the coroutine every frame during which that condition is true, but rather to start the coroutine on the frame during which it becomes true. But I think you’re confusing yourself with A and B. It doesn’t really matter what your condition is. All you care about is:
Was it true previously?
Is it true now?
So your real condition for starting the coroutine should be:
if (myConditionIsTrueThisFrame && !myConditionWasTrueLastFrame)
You just need to calculate those two things. This may involve tracking the previous state of your condition from last frame, which you can do simply enough in a variable.
Hey Thanks for your time, but I’m sorry to say that this is not what I wanted to know. Thats on me cuz I asked it a litle wired.
What I actually need to know is more conserning how to stop and restart the coroutine. A and B are just how how my condition lookes like and could also be just one bool or smth else. I also know how to start it only in the frame where it is met, I just wanted to know whether it is necessary.
The thing is, I’m still confused when exactly a coroutine is active cuz at the moment it lookes like it workes the first time but the second time the coroutine doesen’t seme to start.
The purpose of X is to set a bool to false after like 20 seconds and I want it to stop if the condition changed and restart with 20 s after the conditions are met again.