- Home /
While loop ignores conditions in coroutine
Hi all,
I've got a problem with an infinite while loop that ignores conditions and makes GameObject behave strangly.
Have a look at the code:
private IEnumerator Fall()
{
float timer = 0;
speed = 0;
while (gameObject.transform.position.y > -5)
{
timer += Time.deltaTime;
speed = timer * timer * 20;
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, new Vector3(gameObject.transform.position.x, -10, -5), speed);
yield return 0;
}
Destroy(gameObject);
}
Now, here's what's happening. 1. The gameObject is shaking. 2. It goes down to some certain level (under -10) or falls infinitly. 3. The 'while' loop never ends, however adding
if(gameObject.transform.position.y <= -5)
break;
inside those loop fixes this issue as the object is finally being destroyed.
How can I make it work properly? Is it a common issue with 'while' loops in coroutines?
Hello there,
I use Coroutines all the time, and I've never had this problem... I've just tested your code above in a new project and it works just fine without the break.
I'd make sure the coroutine is only called once by assigning it, like this:
private Coroutine cor_Fall = null;
private void Start ()
{
if(cor_Fall != null)
{
StopCoroutine(cor_Fall);
Debug.Log("COROUTINE WAS ALREADY RUNNING");
}
cor_Fall = StartCoroutine(Fall());
}
Other than that, if you aren't getting errors I'm not sure what's going on.
Hope that helps!
Cheers,
~LegendBacon
Hi @Legend_Bacon, I deal with it like this:
void OnTriggerEnter2D(Collider2D other)
{
if (once && active == 0)
{
StartCoroutine(Fall());
once = false;
}
}
and it also works fine creating only one Coroutine at once. I've tested my code on 3 different computers with different versions of Unity and it was bugged in every case.
@TreyH As you cans see above in OnTriggerEnter2D method. It's 2D enviroment and the GameObject consists of some textures and two ColliderBox2D, one for collision, one for triggering the coroutine.
Check that there is no other code also setting the position of the falling object.
@andyborrell thanks a lot! You were right - I was still moving my platform in Update method of parent class!
Answer by SkorpionX · Jun 28, 2019 at 02:22 PM
The problem was most certainly that other coroutine called in the same class might been unfinished. Can't really reproduce that as it was a long time ago but as I can see the coroutines have been merged as their structure didn't make much sense:
First coroutine was called in an OnTriggerEnter2D method
Second coroutine was called in the first one
The object was supposed to destroy itself at the end but the first coroutine could be still doing it's job
Can't really tell if that was what fixed the issue but one I can tell - it's no more a problem in my code. Hope it will help someone!
@DDeathlonger, your case seems different but as someone stated flipping calls fixed it so maybe it's the right way!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Simple jump code 2D - c# 1 Answer
Co-routines, timing and framerate independence 2 Answers
Can't seem to do a coroutine loop? 1 Answer
Distribute terrain in zones 3 Answers