Stop Game on Exception

Hello Community,

is it possible to “emulate” compile-time errors in Unity? I’d like to exit the Play mode (or even better don’t let Unity enter the Play mode) in case that some references are null (those references are set by drag’n drop in the editor).

When i throw an (uncatched) exception if a reference is null it will be listed in the console but the game keeps running.

Thank You

VR

Very interesting question. I don’t know if you really get a nice workflow with what you want to achieve, but I think I may have found a way to make it actually work.
To catch the Debug.Log and -Error, -Warning stuff you can use this delegate:

Application.LogCallback

However you would need to find a way to register code from an EditorScript, you need access to the UnityEditor namespace to set EditorApplication.isPlaying = false
It may get very involved at that point, mostly though if you run in batchmode. It seems though that you want to run a script just normally while developing with the editor open. I suggest you use just a normal menu item to register the code.

MenuItem

from Unity: Script Compilation:

Unity compiles all scripts to .NET dll files. .dll files will be jit compiled at runtime.

the unity editor already detects unused variales, unreachable code, etc. but i’ve never seen it check for null references (like obvious ones: var a : GameObject = null; Debug.Log(a.tag);). i haven’t seen and found any doc about how to set the editor’s warning/error level.

so i guess runtime is needed to check them, unless you want to fork with your own editor script as tom suggested.

Hi,

i’ve put a simple

if(something == null) { UnityEditor.EditorApplication.isPlaying = false; }

in the Awake() method of my script. This will stop the Play mode nearly instantly (some NullPointer errors will be logged anyway). It’s not that important to me, so i think that will do it. My idea was to give the person who will use my Script a hint if he forgets to initialize/configure the script properly.

Thanks for pointing me in the right direction!

Why not use the “Error Pause” button in the Unity console?