Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Interrupting compile when not completing

Discussion in 'Scripting' started by Entomophagus, Dec 4, 2023.

  1. Entomophagus

    Entomophagus

    Joined:
    Mar 30, 2022
    Posts:
    7
    Hi everyone,

    Following issue, to which help would be greatly appreciated: Suppose I work on some piece of code and at the time of saving I have something broadly equivalent to

    while(/*something always true*/) { ... }

    Maybe I made a mistake in my loop, maybe I habitually saved before having finished, doesn't matter. It happens. The code starts compiling but obviously never finishes. What can I do to stop the compile right there and then?

    My only solution so far has been to force quit the editor. That seems hardly a good solution, especially since I then can't open the project again because it gets stuck when compiling on start-up, leaving me to either go back to an earlier commit or edit the code without having Unity open -- sure, it can be done, but it strikes me as odd that I can't just say "Oops, sorry, stop compiling right now."

    Yes, I know I can turn off the auto-compile on save, but that doesn't change the possibility that I made a mistake inside my while loop when intentionally compiling, or similar.

    Any help would be appreciated. Thanks a lot,
    -Entomophagius
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,238
    An infinite loop shouldn't matter during a recompile, unless said loop actually occurs during the recompile.

    It should only matter when the code is actually running. So if this is code for runtime, then it should only matter when in play mode. Might be useful to know what context this code is expected to be running in.
     
    CodeSmile likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,840
    No, the compiling has finished. Otherwise your infinite loop could not run. However since your code does run it means it has been compiled.

    Infinite loops are in general an issue. There are some quite intrusive but easy to use solutions like the panic button but it seems it's no longer supported and has been removed from the asset store. Though there seems to be alternatives. However as I said those solutions are usually quite intrusive as they inject safeguard code in literally every loop / branch in your code in order to break out of it in case.

    The usual alternative is to attach visual studio debugger so you can pause the execution and manipulate the loop / the current executing line to get out of the loop. There is no general approach that works in all cases. If you have an infinite loop inside Update and you're in playmode, even when you manage to break out of the loop, you would immediately trapped again once the next frame hits. So the general advice is:

    1. Don't write infinite loops in the first place. So watch out what you're doing.
    2. Avoid altering code while you're in playmode. Hot-reload could easily trap you without your intent.
    3. When writing editor code or thing that are marked with ExecuteInEditMode, be extra cautious what you're doing.
    4. Save your scene often. There's always the possibility that the Unity editor crashes for various reasons. So make sure you don't have massive unsaved changes in the editor.
     
    Kurt-Dekker likes this.