- Home /
How do couroutines handle If Statements within a for loop?
Hi There!
I've been trying to make a simple dash function:
IEnumerator Dash() {
for (float i = 0; i < 150; i += 20) {
speed = 110 + i;
yield return new WaitForFixedUpdate();
}
speed = 60f;
for (int i = 0; i < 48; i++) {
if (!dashCanStartAgain) {
yield return new WaitForFixedUpdate();
} else {
yield break;
}
}
dashCanStartAgain = false;
dashKeyTrigger = false;
}
Basically, dashCanStartAgain is changed in another function, and I want that we exit the for-loop through the "yield break". The problem seems to be that the for loop does not seem to notice any changes in the bool, and always goes into the first statement.
Is that the nature of forloops in couroutines or am I missing something? I am sure that dashCanStartAgain IS being changed.
Cheers! Luke
Answer by VMohan · Sep 04, 2015 at 10:18 PM
I think the problem is that you are using yield break instead of break.
According to this post, yield break returns out of your function, which would mean you do not reach line 14 and 15 to reset the dash.
In summary, I would try changing line 11 to be just a break.