- Home /
Do-while loop freezes the engine when running the game
Like the one thing I would think to use the Do-while loop instead of a regular while loop freezes the engine when pressing play :/
Essentially I have the do-while loop set up, so that it would run once, regardless of whether the queue the while part checks for is actually null or not, and THEN have it check it. And I need it this way because the first time something will be added to the queue is within the loop itself.
But even if I take out any enqueuing and dequeuing, all it does is crash, even though by logic, it should run the loop once, then do the check and have the loop end entirely. Adding something in just so the loop runs once as a while loop seems a tad bit stupid with me trying to trick the program to do my bidding. Anyway, here's the code:
Queue<Transform> selectionQueue = new Queue<Transform>();
int depth = 0;
do {
depth++;
} while (selectionQueue != null);
... Actually never mind, I tried having the queue have something set up in it before the loop goes through and the engine still freezes. I don't know what's going on here anymore, but commenting out the loop makes everything work just fine again.
With the few lines of code you have provided, the condition in the while
will always be true, which induces an infinite loop, explaining why Unity freezes.... Is it your real code? If not, provide it please.
Right, also even when enqueueing / dequeueing elements to / from the queue it will always be not null. So the breaking condition will never be true unless you specifically set "selectionQueue" to null inside the loop.
The condition probably should be
} while (selectionQueue.Count > 0);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Starting a single coroutine when multiple methods use the same name. 0 Answers
What could be freezing unity? 2 Answers
Do-While loop 2 Answers