Search Unity

While crashes Unity, then what?

Discussion in 'Editor & General Support' started by MinhocaNice, May 4, 2020.

  1. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Hello, I am quite new to game developing and I started trying to just make a very simple game just to get an idea of everything. I have just a tiny bit of experience in programming.

    I made a C# script that had a while () {} command in it, and the entire editor just stopped. After reading some threads on the internet, I realise my mistake, as the while loop can't be broken ever, and therefore it freezes.

    However, my question isn't about programming but rather about the editor itself.
    In the case of a program freeze, is there any command to halt the game, so the engine can work again?
    I made a lot of changes to my scene, so I didn't want to simply have to go to Task Manager and kill Unity task so I can restart.

    It would be disappoiting if my simple programming mistake just completely crashed Unity.exe without any ways around this. It's not even considered an error at first place, so why does it freeze? I would expect it to freeze the game itself, but the entire editor?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    MinhocaNice and Joe-Censored like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Save scene before hitting the Play button, unless you are specifically messing with things you don't want saved. You can also consider using different types of loops which are less prone to becoming an infinite loop (foreach for example), using a coroutine with a yield (so even if infinite the editor would still get back control during the yield), or adding a fail safe to potential problem loops.

    Code (csharp):
    1. public void ProblemFunction()
    2. {
    3.     int loops = 0;
    4.     while (true)
    5.     {
    6.         CallingSomethingUseful();
    7.  
    8.         //Check for infinite loop
    9.         loops++;
    10.         if (loops > 10000)
    11.         {
    12.             Debug.Log("Hit infinite loop in ProblemFunction, breaking");
    13.             break;
    14.         }
    15.     }
    16. }
     
    MinhocaNice and Vryken like this.
  4. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Thanks @PraetorBlue, that was actually a really fast answer. But I don't quite understand the solution you placed in the other thread.

    A breakpoint is basically just a break; command?
    How do I attach the debugger to the command?
    Currently, my command is this:

    Code (CSharp):
    1.  
    2.         Debug.Log("Player destroyed;");
    3.         while (PlayerObject != null)
    4.         {
    5.             PlayerObject = null;
    6.         }
    7.         FindObjectOfType<GameManager>().LevelLose();
    I placed nearby lines just for reference.
    I am trying some combinations here but none seem to work. Here is what I am trying:
    Code (CSharp):
    1.         Debug.Log("Player destroyed;");
    2.         while (PlayerObject != null)
    3.         {
    4.             int A;
    5.             break;
    6.             PlayerObject = A;
    7.         }
    8.         FindObjectOfType<GameManager>().LevelLose();
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    there is also backup scene in temp/ folder
    (but it gets erased if you start unity again, so need to copy before starting)
     
    MinhocaNice likes this.
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Not quite. A breakpoint is not something that goes in your code. It is a feature of your code editor or IDE. Here's a screenshot of a breakpoint I added to some code in Visual studio by clicking in the left margin of this file:
    upload_2020-5-4_18-28-19.png
    Then to attach Visual Studio's debugger to Unity, you just hit the play button in Visual Studio:
    upload_2020-5-4_18-28-58.png

    Then, if the code reaches the breakpoint while the debugger is attached, the execution is paused and you can debug! You can step through the code line by line with the buttons in the top left, mouse over variables to see their current value, and in the bottom right you can see the immediate window which lets you run any code you want in real time using the current scope:

    upload_2020-5-4_18-31-58.png

    This is all based on Visual Studio though (and my screenshots are from the Mac version, so your interface might look slightly different). Different IDEs have different interfaces but it should be generally similar, and most IDEs have built-in debuggers.

    Debugging is a really powerful tool that can help you analyze your code as it is running to help you pinpoint those really pesky bugs. Just make sure you remember to detach your debugger when you're finished!

    Here's some more generic info on Visual Studio's debugger: https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019
     
    Last edited: May 4, 2020
    MinhocaNice and Vryken like this.
  7. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Uh, I am trying this now... But it's really tricky, I have no idea what I am doing frankly.
    I am pretty sure I just crashed my Visual Studio as well. I cliked the Pause button (two vertical bars) close to the play one and it just froze... Now both the Unity editor and Visual Studio are frozen.
    I gotta eat. I will try again in an hour.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    MinhocaNice likes this.
  9. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Alright, I accidentaly closed my Unity yesterday by closing the Visual Studio, got stressed and went to sleep.
    When I wake up today, after eating and brushing my teeth I realised I didn't lose a lot of progress anyway.

    Thanks for the support. Next time something like this happens, I will try to use these debuggers tools and stuff.