Unity Crashes From While Command

This is the second 'While' Command that has crashed Unity, and i have no idea why. I have them both for you to look at, but here is the latest culprit:

static var eleswitch : int;
var trigger : int = 1;
var Player : Transform;
var EleDoor1 : Transform;
var EleDoor2 : Transform;
var EleDoor1Open : Vector3;
var EleDoor2Open : Vector3;

var openSpeed : int = 5;

function OnTriggerStay (Other : Collider) {
eleswitch = trigger;
Player.parent = null;

function DoorOpen (other : Collider) {
var door1close = EleDoor1.transform.position.z;
var door2close = EleDoor2.transform.position.z;
    while (door1close < EleDoor1Open.z) {
    while (door2close < EleDoor2Open.z) {

Just ask, and i could post the other one for you to look at and see if it is the same thing or not. This is annoying as the 'while' command is very useful, and i like to test each part of my script as i write it, as such it usually crashes before i have saved...a bad habit of mine i guess, but i was wondering if there was a fix to this or not.

If a "While" loop causes a crash or causes Unity to lock up and hang, it's because the loop is never reaching its end condition. It looks to me like you're causing an infinite loop. The reason being that you're checking:

(door2close < EleDoor2Open.z)

....to see whether the loop should finish, but you're nor modifying either of those two values inside the loop, so it will always be true and never finish executing.

This may come from a mistaken assumption that the earlier line:

var door2close = EleDoor2.transform.position.z;

....creates a link, or a "reference". It does not - it in fact just copies the value into "door2close". So if you subsequently modify "EleDoor2.transform.position.z", those changes won't be reflected in the "door2close" variable.

Basically to solve the "crashing" problem, you need to ensure that the condition specified in the "While" loop can be met, during the execution of the contents of that loop.


There appears to be numerous other problems with your code which indicate that the general approach you are taking to achieve your goal might be flawed.

So, while I hope this particular answer may help you to understand why a "While" loop can cause Unity to freeze up (and therefore on a broad level, answers this specific question) it won't necessarily help you all that much towards completing your script as a whole.

Might I suggest starting a new question, and ask a broader question aimed at finding the best technique to achieve your particular goal? Such as:

"How can I make a door open and close when a collider is touched?"

(if that happens to be your goal)

You could then optionally post the code that you have so far, and it would be more relevant for people to post alternative suggestions to how it could work, without having to guess at the bigger picture.

Everything that the previous person said is on right track, but you also need a yield statement!

Put this


at the end of each loop. And you will notice that your program doesn't crash. You will still need to make sure you have your conditional logic correct.

Looks like you just needs to use an if instead of a while (since you are calling it every frame anyway), but still all the above concerns apply.

duck is right but i think there is another problem. OnTriggerStay might be executed several times and it will execute your loop several times too. it's a really infinite or a big loop that might take several seconds so the unity will crash. why don't you use OnTriggerEnter or OnTriggerExit?

Please refer to:


Your other question for a related solution.

You can use (for javascript)

while (condition) {
yield WaitForSeconds (0);


while (door1close < EleDoor1Open.z) {
    yield WaitForSeconds (0);
while (door2close < EleDoor2Open.z) {
    yield WaitForSeconds (0);

you should not used OnTriggerStay as they said above

I used a while loop in a coroutine, and after the while loop was executed once, I added the line

yield return null;

This fixed the crashing for me