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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to quit from script while the editor is not responding

Discussion in 'Scripting' started by rainboww, Jun 3, 2018.

  1. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    hello,
    i have a app that sometimes causes loop or hangs so i made a function to end the execution, but not the editor it in some way from external input with the script. I do know the following is executed but it does not end the app execution, however it worked with Application.Quit(); untill some update:

    Code (CSharp):
    1.  
    2.         static public void quitprogram() {
    3.             Debug.LogError("quitprogram");
    4.             UnityEditor.EditorApplication.isPlaying = false;
    5.             File.AppendAllText(Path.Combine(p.gamesetpath, "quitapp.glo"), "endapp\n");
    6.             Application.Quit();
    7.             UnityEditor.EditorApplication.isPaused = true;
    8.             go.script.GetComponent<execute>().enabled = false;
    9.             go.script.SetActive(false);
    10.         }
     
    Last edited: Jun 3, 2018
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Maybe you are infinite looping someplace. I don't think input works at that point because it never gets to the next frame.
     
    RavenOfCode likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I'm inclined to agree with fire7side's comment. This sounds like a problem you really need to find and fix properly. I'd recommend running in the debugger. When the 'hang' occurs, you can pause execution (inside the debugger, not the Unity Editor) and then examine all the threads. You may be able to find the root cause that way.
     
  4. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    I know that i had a loop with this one the problem is that while i tried to fix it i liked to end the execution inside editor instead of end editor with the task manager. I had to restart editor 5 times till it was fixed thats why i want to have a loop breaking quitapp...
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I would strongly, really, most strongly, urge you to use the debugger. :)
     
  6. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    You mean the visual studio debugger?

    Beside this it was possible to do this once. So in that point the new unity version is less capable than the one before.
     
    Last edited: Jun 4, 2018
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    If Unity gets stuck in an infinite loop because of your code, it's pretty much dead. I haven't ever seen a way to quit this. Now, if your loop is in a coroutine that yields, you can usually stop those. But just a pure loop, it is stuck. You just have to solve which loop is creating the problem.
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Or use the debugger. :)
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Which is part of solving. How you do it, is completely up to the person. :) If I make a infinite loop mistake, I can tell which one is doing it.
     
  10. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I was responding more to the comment "I haven't ever seen a way to quit this." I should have been clearer in the part I quoted. :)
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Ah, got it!
     
    Doug_B likes this.
  12. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    I still dont understand to which debugger you refer. I use visual studio, it comes with a debugger, however i had problems with it not attaching to unity thats one reason i would like to not use it.

    Also the problem i had was not a loop but some functions which would call themselfes in a loop over some 5 other functions there its not so easy to find the problem.
     
    Last edited: Jun 7, 2018
  13. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    719
    You can use Notepad++ as a backup when this happens to edit your code.

    What you need to do in this case is backtrack, comment OUT the code that you've got there COMPLETELY in your script and activate only one tiny chunk at a time, get out the pest control.

    It's always nice to have a backup code editor for this kind of thing, honestly it's happened to me once when I was using [ExecuteInEditor]. I put EditorUtility.DisplayDialogue in there, and then the Unity Editor kept displaying it over and over, so I needed to crack out notepad++, open the .cs file in my project folder and grey out the entire script, rethink it and move on.
     
    Last edited: Jun 7, 2018
  14. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Yes, the Visual Studio debugger. I use the "Attach To Unity And Play" option. The only times I have had problems connecting the debugger is when:
    1. A build has just completed in Visual Studio but not yet completed in Unity. Then you need to stop debugging and wait for Unity to catch up before trying again.
    2. If you debug one session and stop the debugger during the startup phase (e.g. during an Awake method in the opening scene) at that point Unity will carry on running. Then I need to remember to stop Unity before I try and debug again.
    I must admit, the first point there took me a while to figure out. :) But otherwise, I have found it integrates quite smoothly with Unity. The only real delay to using it is the initial load time of Visual Studio.

    Maybe consider giving it another try to see if it helps you out here at all?