- Home /
How to use a WHILE LOOP? Obviously not inside ***function Update***
I used a while loop as follows:
//while myBoolean is true, I want the gameObject's tag to become "HotObject" (and
//obviously when myBoolean turns false, the tag will be reverted back to what it was).
function Update () {
while (myBoolean) {
gameObject.tag="HotObject";
}
}
As many of you must have already guessed, this freezes Unity. I searched for relevant questions a bit and I learned that while loops inside function Update freeze Unity. So how would I use the aformentioned loop? (and a happy 2011 everybody!)
Your script's Update function effectively runs inside a "while (playing)" loop, controlled by the Unity engine. So in your case you really just want this -- if (myBoolean) { gameObject.tag = "HotObject"; }
As I said to Vicenti, my initial approach was the use of if...else but for some reason that did not work and I ended up solving my problem in another way. I guess what you guys say is totally correct but something's wrong with my scripts. For now the solution I found works just fine. Thanks a bunch for commenting yoyo!
Answer by Ejlersen · Dec 31, 2010 at 05:56 PM
Okay, lets take a look at the while-loop:
while(condition)
{
code
}
A while-loop keeps executing its code, while its condition is true. E.g.
var i : int = 0;
while (i < 10) { Debug.Log("Index i is: " + i);
i++;
}
This will print:
Index i is: 0
Index i is: 1
Index i is: 2
Index i is: 3
Index i is: 4
Index i is: 5
Index i is: 6
Index i is: 7
Index i is: 8
Index i is: 9
It stops there, because i = 10 does not satisfy the condition: i < 10.
You can also do this with an for-loop, e.g.:
for (var i : int = 0; i < 10; i++)
{
Debug.Log("Index i is: " + i);
}
A while loop in Update() does not make the program "freeze", but given the condition you gave the while-loop will make it loop forever, until its condition is false.
I believe you mean that the while loop does not necessarily freeze the program if inside Update and whether it will cause it to freeze or not depends on it's content. In my case it freezes Unity :( So you say that using while inside Update is generally ok, given one uses it carefully? I tried using if but it didn't work. I also tried calling a function that changes the tag, yield-waits for a specific amount of time and reverts it back to normal but this works only the 1st time myBoolean becomes true.I'll leave it for the next year as It's getting late for that. Hey Ejlersen,happy 2011!
Correct, while-loops does not freeze the program, if used correctly. An infinite loop will freeze your program in all cases. Happy new year :)
so if I use the following, then how does one break out of it??
function $$anonymous$$oving() { while (true) { //some code } yield WaitForSeconds(5.0f); }
Answer by Loius · Dec 31, 2010 at 06:25 PM
Infinite loops freeze Unity, no matter where they are. (Actually they just crash it; a much nicer alternative. thanks devs!)
If you want something to be true while something else is true, you don't use while. Update is its own "while( game playing )" function (of sorts). You use ifs:
if ( ready ) tag = awesome;
else if ( !ready ) tag = notawesome;
Whiles (as explained above) are for conditions that exit eventually -
while ( transform has children ) {
AFunction( childZero );
RemoveChildZero();
}
Thanks a lot Vicenti :) This helps in understanding "while" use. $$anonymous$$y initial approach was the use of if...else but for some reason that did not work and I ended up solving my problem in another way. Thanks again!
Hmm, I have a load of while(true) loops (naughty, I know) in my current project and I haven't had a crash. I'm calling them from start, when I try using any kind of while loop in update it crashes on me... which is understandable as the update function is basically a big while loop!
You propable use coroutines which is a totally different story. Unity uses only one thread, so all code runs sequential. If you have any kind of code that get stucks in one place, everything else (in this thread) will be freezed. Coroutines can "break out" of any kind of loop with yield. This keyword will yield the control back to Unity.
Not entirely true..
I agree that infinite loops DO freeze Unity, but Unity also freezes after tries for a loop. I had a program where a while loop checked on a 75% chance and even that made Unity freeze.
Answer by VillainT_ · Nov 27, 2012 at 03:41 PM
You can use (for javascript)
while (condition) {
//codes
yield WaitForSeconds (0);
}
but not use on update function
Your answer
Follow this Question
Related Questions
or doesn't work in while loops? 1 Answer
How do I make something rotate until a condition is met 1 Answer
While/if loop problem 1 Answer