- Home /
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; DoorOpen(Other); }
function DoorOpen (other : Collider) { var door1close = EleDoor1.transform.position.z; var door2close = EleDoor2.transform.position.z; while (door1close < EleDoor1Open.z) { EleDoor1.transform.Translate(-Vector3.right*Time.deltaTime*openSpeed); } while (door2close < EleDoor2Open.z) { EleDoor2.transform.Translate(Vector3.right*Time.deltaTime*openSpeed); } }
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.
Rather than just asking people to "help me fix this!", maybe you could provide some additional information?
Do you actually understand how a while loop works? Doesn't look like it to me. You need to go back to basics and read about how while loops actually work, because you are using them in a way which is guaranteed to cause problems.
While loops not only repeat until the condition is met, but they also halt the interpreter from moving onto the next piece of code. The interpreter simply won't call the next statement until you break out of the while loop. So your second while loop doesn't even get executed at all, because it will never get past the first while. loop.
Answer by duck · Mar 02, 2010 at 05:37 PM
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.
However...
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.
Changing it to 'while (door2close < EleDoor2.transform.position.z)' does not work, as it will never run becasue they start out at both being the same number, thus door2close will never be less than it. Also if i were to use
Please see the additional text I have added to my answer, under the title "However..." :-)
Answer by lowbloodsugar · Mar 03, 2010 at 04:19 AM
Everything that the previous person said is on right track, but you also need a yield statement!
Put this
yield;
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.
The "previous person" is not really relevant here.. because the order of the answers might change due to order by rates.This would be better as a comment so @Duck could also change in his answer. Regards
Answer by Waz · Jun 08, 2011 at 04:42 AM
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.
Answer by Ashkan_gc · Mar 03, 2010 at 07:17 AM
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?
Answer by Motionreactor · Mar 03, 2010 at 03:44 PM
Please refer to:
http://answers.unity3d.com/questions/5045/using-a-gui-button-to-stop-a-gameobject-movement
Your other question for a related solution.