Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by schwertfisch · Dec 31, 2010 at 05:39 PM · updatefreezewhilewhile-loop

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Jan 01, 2011 at 02:09 AM 0
Share

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

avatar image schwertfisch · Jan 01, 2011 at 08:09 AM 0
Share

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!

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image schwertfisch · Dec 31, 2010 at 06:46 PM 1
Share

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!

avatar image Ejlersen · Dec 31, 2010 at 07:52 PM 0
Share

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

avatar image RandomCharacters · Sep 04, 2013 at 07:42 AM 0
Share

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

avatar image
7

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(); 
}
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image schwertfisch · Jan 01, 2011 at 08:07 AM 0
Share

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!

avatar image POLYGAMe · Mar 23, 2012 at 08:59 AM 0
Share

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!

avatar image Bunny83 · Mar 23, 2012 at 10:37 AM 0
Share

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.

avatar image Reizla · Jan 29, 2015 at 07:11 AM 0
Share

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.

avatar image
0

Answer by VillainT_ · Nov 27, 2012 at 03:41 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get something to be inactive while there is a certain value 2 Answers

Can I use a while() in the function OnDrawGizmos() ? 1 Answer

Unity Crashes From While Command 7 Answers

on mouse enter without function update 2 Answers

unity bug??? while loop is satisfied, but doesn't quit 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges