Unity locks up on me

is there some sort of setting or something to help prevent lockups
I know I have a few bugs in my script but it gets annoying when it locks up and I have to kill it in the task manager.
seems like if it gets stuck in a loop or something I should just be able to stop the game and see where the problem lies.

I know it lies in my loop or my switch statement

I’m running 2.6 in Win 7 64bit

thanks :slight_smile:

Ideally, the editor should never lock up but rather throw out an error if your script has issues, at least that’s what I’ve been told by the UT techs. Submit a bug report and be sure to include a crash log (if there is one).

If you get stuck in an indefinite loop it will crash. This is because it cannot continue, or yield to a background process(unless you explicitely make a coroutine and tell it to yield).

Check all loops, particularly while loops and make sure that they all escape somehow.

this is my while loop

GameObject rowCheckLeft = GameObject.FindWithTag(rowLeftNext);
        while (rowCheckLeft == null)

its a space invader game where I put a tag for each row so if the row is gone it will move a collider wall father away to have the new row of invaders go to the edge of the screen. if I am thinking right it would only be infinite if I killed them all but it locks up sooner on me
inside of this while I have a switch that reassigns the tagname to the new row name
is that a poor while loop setup and not working like I think
I am new to Unity and scripting in C#
thanks

While loops should be used with care.

The problem that is arising is that you are not allowing it to change within the while loop.

Code executes in a linear order, and therefore if rowCheckLeft is null once it will be null forever. While in a while loop, you cannot do ANYTHING until you leave it.

Follow the logic, and think how will it get out of
while
{

}

remembering that the game will not advance until it does (i.e. you cannot do a while to wait for an in game occurence).

Also, in a while loop like that which is infinite by nature, add in a yield statement to allow the thread to do other work, like yield wait for seconds 1 or something.

I found the flaw that made it infinite I have 2 sets of these whiles one to check left and one to check right. I did a copy and paste and missed changing a value in my new switch statement.

GameObject rowCheckLeft = GameObject.FindWithTag(rowLeftNext);
        while (rowCheckLeft == null)
        {
               
                //finds the right wall for the colider and sets its new position
                GameObject MoveLeftWall = GameObject.FindWithTag("leftWall");
                MoveLeftWall.transform.position = new Vector3(movingLeftWall, 10, 0);
                movingLeftWall -= newWallLocation;

                if (rowDecrease < 1)
                    break;

            switch (rowDecrease)
            {
                case 1:
                    rowLeftNext = "row1";
                    --rowDecrease;
                    break;
                case 2:
                    rowLeftNext = "row2";
                    --rowDecrease;
                    break;
                case 3:
                    rowLeftNext = "row3";
                    --rowDecrease;
                    break;
                case 4:
                    rowLeftNext = "row4";
                    --rowDecrease;
                    break;
                case 5:
                    rowLeftNext = "row5";
                    --rowDecrease;
                    break;
                }
            //after rowRight has been reassigned makes sure the next row is there or not
            rowCheckLeft = GameObject.FindWithTag(rowLeftNext);
        }

I had rowIncrease instead of decrease in the switch statement

I didn’t think it was a infinite loop except on programmer error :sweat_smile: I know that I need a code to stop it entering the while loop after I kill all the invaders.

so basically if I get stuck in a infinite loop on accident it will just lock up Unity. guess I better pay more attention to my code
:smile:

thanks for the responses

1 Like