[SOLVED] Is unity DEBUG preprocessor always defined? Yes, by design.

Some versions ago (now I’m with 5.3.4f1) I was adding and removing DEBUG preprocessor in the PlayerSettings → Scripting Define Symbols, to include/exclude some debug only code that also causes memory allocation and lower performance.

Now even if I remove it, the debug code is always ran.
The build platform is set as iOS, and doesn’t matter if I set to Release mode and uncheck the Development Build. The DEBUG preprocessor is always set.

Is there any way to fix/resolve this and have the behavior as before?

:frowning: nobody?

If the DEBUG symbol is present in your release build, then that’s a bug.

Issue a bug, include a simple project recreating it if you can.

If you can’t recreate in a bare-bone project, then there may be something wrong in your code. You should look into the .proj files in your project folder and make sure they don’t have them in the ‘release’ settings.

I’m not saying that is the release build (I will check too, but I don’t think is doing it). I have this issue in the editor.
I was enabling/disabling the “debug” code in the editor, but now I can’t disable it anymore.

Oh… you’re enabling and disabling in editor…

you have to rebuild if you change it, make sure everything recompiles when you swap the flag

1 Like

Unity has some built in preprocessor directive.

For a “debug” build (Development build checked in editor) there is a DEVELOPMENT_BUILD symbol. This will eliminate the need for you to manually define it.

3 Likes

I try to reformulate my problem, because it seems i wasn’t clear enough.

I’m using this code for testing:

#if DEBUG
Debug.Log("DEBUG");
#endif

#if DEVELOPMENT_BUILD
Debug.Log("DEVELOPMENT_BUILD");
#endif

And I’m changing these settings:
2621542--184096--Screenshot 2016-05-04 09.41.13.jpg
I tried already all the possible combination of “Run in XCode as” Release/Debug, “Developement Build” on/off, and adding and removing from “Scripting Define Symbols” the DEBUG symbol. (And yes, I’m recompiling every time)

Whatever I tried, when I hit play (not running a build) I always get this output: “DEBUG”.

So my question is: How to remove the DEBUG symbol from the defined ones?
Some versions ago of Unity (I don’t know when) I was able to add it and remove it, and my code was working correctly.

1 Like

I think I found the problem. I searched in the whole solution and I found a plugin that I added some time ago that has this code:

#if UNITY_EDITOR
#define DEBUG
#endif

Not very happy of this… I’ll see what to do now.
Thank you for your help.

3 Likes

That sucks…

Which plugin?

I already wrote on their forum.
Anti Cheat Toolkit, and the developer already answered that will fix it soon in the next release
Google Universal Analytics. Waiting for their answer.

Anyway, I changed their code and I still get the debug code executed. I can’t figure out why.
So I ended up changing my code. Now I use BRA_DEBUG and finally it works as expected.

I also tried a new clean empty project, with no symbols defined anywhere.
Still #if DEBUG is true.
Very weird.

I filled a bug report to Unity. Case number 794092

2 Likes

About the issues in the plugins, is correct to say that #define has a file scope. It means that it could not anyway create a global defined DEBUG symbol. The only possible minor issue is that a plugin could start emitting some additional unexpected log, nothing else.

So in case someone will have the same problem, I received an answer from Unity:
“The editor is always in DEBUG by design. There are many different functions and messages for debugging which by default work on editor while on standalone or any platforms you need to enable development build for that.”
So #if DEBUG seems to be the same as #if UNITY_EDITOR

17 Likes

Thanks, this was helpful!

For anyone stumbling across this thread, a small correction:

Not quite.

DEBUG seems to behave like “UNITY_EDITOR | DEVELOPMENT_BUILD”, i.e.:

void Start(){
#if UNITY_EDITOR
Debug.Log(“This code is executed in the editor only.”);
#endif
#if DEVELOPMENT_BUILD
Debug.Log(“This code is executed in development builds only.”);
#endif
#if DEBUG
Debug.Log(“This code is executed in the editor as well as in development builds.”);
#endif
}

17 Likes

I was curious about this topic, and stumbled across this post.

Correct me if I’m wrong, but to disable DEBUG, can you just put this at the top of a file?
#undef DEBUG

From my quick test, it prevents Unity from executing any DEBUG directives. Looks like it works across all files, too, even though I only added the directive to the (absolute) top of one file.

Also, as expected, it disables methods modified by [System.Diagnostics.Conditional("DEBUG")]

Another note, you could wrap the undef in another directive conditional on a symbol you have better control of.