While Loop won't end when SendMessage was executed..

Hey and thanks for reading my thread. I have a quite short question and (I think) it is related to the mechanics of Unity , so Id like to ask here and hope to find an answer.

I ran into a weird problem. When I send an input (for example I press the escape button), I start StartGrowing() which does the following:

    while (!stop)
    {
        AddSprite(block, Xpos, Ypos);
        Ypos += 16;
    }

Good, now stop is false on start , Xpos is 0, Ypos is -1040 So my little while loop should start adding block sprites (which are 16x16 and contain a collider) one above another (+16 on Y axis). I have set up a wide capsule collider which is located at about Y = 0.

On that collider I have a script that seeks for OnCollisionEnter, and as soon as it is triggered, it does a SendMessage(“VerticalHit”) to my main script.

I have checked if this works by manually colliding with it and it worked like a charm. This is how VerticalHit() looks like:

    stop = true;

Basically here is what all this above does (or well should do):

“Wait for the user to press escape key”
“Set the bool ‘Stop’ to false”
“When escape key is pressed, do StartGrowing()”
"StartGrowing() builds blocks one above another until Stop == true;
“At the same time, the script inside the collider checks for collision”
“If one of the blocks hits the collider, it does a SendMessage(“VerticalHit”) to the main script”
"VerticalHit sets the bool ‘Stop’ to true;
“The while loop in StartGrowing() stops because the criteria (!stop) is not true”

Correct me if I coded an error, but could it be that Unity can’t handle a while loop and another script (for example my collider script) at the same time? The above mentioned freezes Unity… and I have no idea why. As I said, I checked manually by adding block by block if the SendMessage() does what it should do, everything works fine.

Hope I’ll find the answer. Thanks in Regards ~Ilhan

Unity can handle loops just fine; it’s basic logic. The problem is that your while loop is infinite and nothing will stop it from running, therefore Unity freezes, and can’t progress past the loop. Setting “stop” to true at some point in the future won’t stop the while loop from running forever, because Unity will never get to the point where stop is true. It needs stop to be true while the loop is actually running, inside the loop itself, not outside. A correct loop has a condition that causes it to end:

var count = 0;
while (!stop) {
    AddSprite(block, Xpos, Ypos);
    Ypos += 16;
    if (++count == 10) stop = true; // or something
}

After the loop has finished, then Unity can continue with the rest of the code. Remember that all scripts are run in a single thread, in a linear fashion.

You could use a coroutine to exit the method every frame (after creating n blocks). This way Unity will have the possibility to check for inputs, collision and so on.

For example, let’s say that you want to create 10 blocks each frame.

function StartGrowing() {
    while (!stop) {
        var count = 0;
        var stop = false;
        while (count < 10) {
            AddSprite(block, Xpos, Ypos);
            Ypos += 16;
            ++count;
        } // end while
        yield; // wait for the next frame
    } // end while
} // end function

function VerticalHit() {
    stop = true;
} // end function