Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

while loop without break crashes Unity

Discussion in 'Scripting' started by beatniq, Oct 7, 2014.

Thread Status:
Not open for further replies.
  1. beatniq

    beatniq

    Joined:
    Sep 28, 2013
    Posts:
    2
    Hello I am having trouble with a while loop. I have two while loops within a while loop. The code looks something like this:

    While (x)
    {
    While (y)
    {
    !y;
    break;​
    }
    While (!y)
    {
    y;
    break;​
    }
    if (!x)
    {
    break;​
    }​
    break;
    }

    I want the code to run the through the main while loop until x is false. However Unity crashes unless I put in the final break ,thereby breaking from the main while loop when I don't want to break from it. Why cant/won't unity just keep running the while loop until X is false and thereby leaving the loop when I want it too. Hope my question is clear.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    What part of infinite loops don't you get? :) The problem is in your code, what you're doing here is really bad code.
     
    Ryiah and Joe-Censored like this.
  3. anonymousunitycreator

    anonymousunitycreator

    Joined:
    Sep 20, 2014
    Posts:
    43
    Lol hippo. I literally read the title and thought the same thing.

    Here beatniq,
    Your issue is that you're never actually setting x to false.
    Explanation:

    Code (CSharp):
    1.  
    2. While (x) //YOU NEVER SET X TO FALSE DURING THE LOOP
    3.     {
    4.         While (y)
    5.         {
    6.             !y; //YOU CAN'T SET A BOOLEAN THIS WAY, AT LEAST NOT IN C#.
    7.             //BEST TO DO y = false;
    8.             break;
    9.         }
    10.         While (!y)
    11.         {
    12.             y;
    13.             break;
    14.         }
    15.         if (!x)//THIS WOULD NEVER HAPPEN, AS NOTHING HAS SET X TO FALSE
    16.         {
    17.             break;
    18.         }
    19.         break; //BREAK DOES NOT MEAN TO STOP THE WHILE LOOP, JUST BREAK OUT OF _THIS_ INSTANCE.
    20.         //BUT THE SECOND IT BREAKS, IT HAPPENS AGAIN BECAUSE X IS STILL NOT SET TO FALSE.
    21.     }
    22.  
    23.  
    24.  


    Try this:
    Code (CSharp):
    1.  
    2. private bool x = true;
    3.     private bool y = true;
    4.     private bool z = true;
    5.  
    6.     while(x)
    7.     {
    8.         print(x);
    9.         while(y)
    10.         {
    11.             //DO STUFF HERE
    12.             y = false;
    13.             print(y);
    14.             break;
    15.         }
    16.         while(!y && z)
    17.         {
    18.             //DO STUFF HERE
    19.             z = false;
    20.             print(z);
    21.             break;
    22.         }
    23.         while(x && !y && !z)
    24.         {
    25.             //DO STUFF HERE
    26.             x = false;
    27.             print("CLOSED OUT X");
    28.             break;
    29.         }
    30.     break;
    31.     }
    32.  
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    if x is a boolean, and you never change its value inside the while loop to something other than it... the loop will go forever.

    How would it ever change?

    If you want this to be a set of code that repeats from frame to frame. Then you need to be putting this in a coroutine and yielding null at the end of the while loop.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Some people seem to be under the impression that "while" loops somehow run over multiple frames. Thinking this, they're under the impression that kicking off a while loop and then doing something elsewhere that would set their check to false is valid.

    There are certain environments where that could be the case, but Unity's main thread isn't one of them.
     
    Reedex, Joe-Censored and AlanMattano like this.
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    I'm feeling you don't know what rhetorical questions are.

    Considering I answered my question in the following sentence with the very same context that you bring up.
     
  7. xXudon

    xXudon

    Joined:
    Jan 10, 2020
    Posts:
    2
    What a nice moderator really helping my homie out here.
     
    HannesTamm123 and SeerSucker69 like this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,416
    Wondering why you need to resurrect a post from 2014 without adding anything useful too. ;)
     
  9. PostleService

    PostleService

    Joined:
    Feb 28, 2021
    Posts:
    3
    Wow. What a f*ng vipers' nest. Thanks for nothing.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,416
    You are not helping here either. I have no idea why you felt compelled to post here and even think it somehow relates to you.

    Let's move on shall we.
     
Thread Status:
Not open for further replies.