Search Unity

How to cancel running a game?

Discussion in 'Getting Started' started by quirkylemon103, Nov 24, 2021.

  1. quirkylemon103

    quirkylemon103

    Joined:
    Aug 1, 2018
    Posts:
    13
    so I accidently created an endless loop at the beginning of the game so this thing wasn't going away
    upload_2021-11-24_12-27-9.png
    I had to force quit unity and re-open it btw my pc is very slow so this takes awhile.
    Is there a way to cancel without closing all of unity?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Nope. When you create an infinite loop like that, the process locks up the main thread and doesn't afford you an opportunity to jump in and tell it to stop. The only thing you can do is terminate the process and restart it.

    Don't worry... we've all done it. :p
     
    Ganicuus and Joe-Censored like this.
  3. quirkylemon103

    quirkylemon103

    Joined:
    Aug 1, 2018
    Posts:
    13
    ok thank you.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you find yourself doing this frequently, you could add code which automatically ends the loop. I do this when I'm troubleshooting an infinite loop, or I'm writing a loop which depends on me not breaking some other code somewhere else, which I might forget about the potential issue months later. Example:

    Code (csharp):
    1. int loops = 0;
    2. while(variable < 10)
    3. {
    4.  
    5.     //something here that was supposed to eventually get variable under 10, but actually never does
    6.  
    7.  
    8.     //check if we're stuck in an infinite loop
    9.     loops++;
    10.     if (loops >= 10000)  //check if we've run this loop 10k times already
    11.     {
    12.         Debug.LogError("Oops I did it again -Britney");
    13.         break;  //ends the infinite loop
    14.     }
    15. }
    I generally favor using loops like foreach instead, since they are not capable of becoming infinite loops.
     
  5. quirkylemon103

    quirkylemon103

    Joined:
    Aug 1, 2018
    Posts:
    13
    Thank you! I will definitely use this.
     
    Joe-Censored likes this.