x


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!)

more ▼

asked Dec 31 '10 at 05:39 PM

schwertfisch gravatar image

schwertfisch
370 53 57 68

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"; }

Jan 01 '11 at 02:09 AM yoyo

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!

Jan 01 '11 at 08:09 AM schwertfisch
(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

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.

more ▼

answered Dec 31 '10 at 05:56 PM

Ejlersen gravatar image

Ejlersen
1.3k 1 6 11

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!

Dec 31 '10 at 06:46 PM schwertfisch

Correct, while-loops does not freeze the program, if used correctly. An infinite loop will freeze your program in all cases. Happy new year :)

Dec 31 '10 at 07:52 PM Ejlersen
(comments are locked)
10|3000 characters needed characters left

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(); 
}
more ▼

answered Dec 31 '10 at 06:25 PM

Loius gravatar image

Loius
10.6k 1 11 41

Thanks a lot Vicenti :) This helps in understanding "while" use. 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. Thanks again!

Jan 01 '11 at 08:07 AM schwertfisch

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!

Mar 23 '12 at 08:59 AM POLYGAMe

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.

Mar 23 '12 at 10:37 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

You can use (for javascript)

while (condition) {
//codes
yield WaitForSeconds (0);
}

http://forum.unity3d.com/threads/160337-How-to-use-while-loop-without-crashing-the-game?p=1096760#post1096760

but not use on update function

more ▼

answered Nov 27 '12 at 03:41 PM

VillainT_ gravatar image

VillainT_
1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x494
x177
x60
x17

asked: Dec 31 '10 at 05:39 PM

Seen: 11606 times

Last Updated: Nov 27 '12 at 03:41 PM