- Home /
#if 0 causing a Unity 2018.2.2f1 freeze on Mac OS X
Coming from a C and C++ background with very little knowledge of C# and being very new to Unity, I tried to comment out some code with an #if 0
preprocessor directive, just like I'm used to do in C and C++.
This caused Unity 2018.2.2f1 to freeze under Mac OS X 10.13.6.
Is this a known feature, a bug, or what? If it is a bug, where can I check if has already been reported? If it has not been reported, how do I report it.
Context: I'm porting dbus-csharp (https://github.com/steffen-kiess/dbus-sharp) to Unity, in order to connect Unity to BlueZ under Linux, for BlueTooth communication.
I'll assume you did ter$$anonymous$$ate the #if
block with #endif
...
For me, the compiler tells that #if 0
is an error, because a "single-line comment or end-of-line expected". That is, it seems you cannot use constants number literals for for this pre-processor directive (I tried it with other numbers as well, even float
and double
).
However, #if false
does seem to work as expected and it compiles successfully. Note, though that I'm on Windows 10, with Unity 2017.3.1 and Visual Studio 2017.
Thank you @Harinezumi !
Sure I did ter$$anonymous$$ate the #if
:-) (I've been program$$anonymous$$g with C since 1982, or so...)
Thanks for the #if false
. I'll use that.
Still, I don't think Unity should freeze. Should I try to report that somewhere? All the code is public (in github) so that I could produce a test case there.
Yes, based on your description, I thought you had a lot of experience :)
I also think it is weird that it freezes, it should give a compiler error at most. You can use the "Help" menu, "Report a Bug..." option in Unity to report a bug. Note that it will try to attach the whole project, so it might help if you can reduce the size.
Answer by Bunny83 · Aug 08, 2018 at 07:40 AM
Pre-processor directives work different in C# than in C / C++. While the C / C++ preprocessor allows you to actually define macros / arbitrary replacement symbols, including other files, provide line meta data and to define conditional compilation sections, the C# compiler is more limited. In C# you don't have an actual preprocessor but the directives are handled by the compiler directly.
In C / C++ a define is basically just a replacement value / macro. However in C# a define is just a symbolic name that doesn't have a value. The #if statement does not allow numeric values, only define symbols. The condtions are restricted to pure boolean logic. So you could just use
#if false
// never included
#endif
#if true
// always included
#endif
But you can also use any symbol that isn't defined like
#if ThisWillNeverBeDefined
...
#endif
Note that conditional compilation was never meant to comment out a section of code "manually". For this you can use block comments. Conditional compilation can be used when you have a configuration switch which defines which behaviour should be used
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity freezes when settings QualitySettings.VSyncCount = 0 1 Answer
Using Pipes/Streams Freezes Unity 1 Answer