A note of the wild use of “if(object)” in Unity’s C# code

Looking at other people’s C# code for Unity, I kept seeing this bizarre usage of if:

if (object) { …}

where object was a real object, not a bool instance. This is a JavaScript kind of usage, where there is a big (perceived) mess about object and value types, so I wondered how did C# “automagically” cast to bool? You cannot do that in classical typed OOP languages like Java.

Turns out there is nothing about this in C# itself, its MonoBehaviour that implements implicit boolean casting

public static implicit operator bool($classname$ me){
return me != null;
}

Actually you will have to navigate the hierarchy up to Unity’s Object to find it. See also here.

But if you are learning C# with Unity, be careful as “This can be really confusing because that behavior doesn’t carry over when you start using standard .net libraries and other 3rd party code that is agnostic to Unity.” – here.

And neither it will when writing your classes, when not extending MonoBehaviour, so its not a healthy habit to catch.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, serious games.

Social Share Toolbar

Comments

  1. Um, no.

    This comes from C. It’s nothing special about Unity per se. Java got rid of it because it’s bad programming practice.

    But C++ keeps it (from C), and many C++ programmers are addicted to it. Since Unity is written in C++, it’s no surprise that someone (at some point) on the Unity team decided to “enable” this paradigm that is still there (and widely used) in the C++ layer.

    Many (former) C programmers specifically hate Java for “making” them do the extra work of typing “if( object != null )”. They see it as pedantic.

    (they’re right, but … you have to weigh it against the maintenance cost of C-style ‘if’, which turned out to be fricking huge. Refactoring becomes much harder, operator overloading becomes more dangerous, etc. Whih is why many people try to move away from if(object-ref) these days…)

    • if( object ) is perfectly valid and correct thing to do in c. if( object ) is always identical to object ( != NULL ) under any implementation. You must also remember that in c the “object” is just a pointer.

      • What exactly does the text mean?

        Is this allocated?

        Is it initialized?

        Is it true?

        What happens when the input is a void* ?

        …chaos.

        It’s a bad habit and – IME – has caused far more lost money in codebases than it has saved in the few letters you avoid typing (auto-complete, remember).

        • What exactly does the text mean? It’s literally identical to if (object != NULL).

          The only thing I can see possibly going wrong is that an uninitialized pointer will be true, but that’s a C problem unspecific to this particular shorthand expression.

          What happens if it’s a void*? Umm… I mean….the same exact thing as if it was an int* or a char* or a struct foo* or a void (foo*)(int) or any other pointer in C. Not really seeing what the problem is. Using the long form expression doesn’t prevent any problem you might encounter with the shorthand expression.

Leave a Reply to reader Cancel reply

*