|
I used a while loop as follows:
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!)
(comments are locked)
|
|
Okay, lets take a look at the while-loop:
A while-loop keeps executing its code, while its condition is true. E.g.
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.:
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!
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)
|
|
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:
Whiles (as explained above) are for conditions that exit eventually - 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)
|
|
You can use (for javascript) but not use on update function
(comments are locked)
|

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!