If I’m doing null checks on game objects, which one should I use? Are they different in any way?
Those are actually opposites.
“if (gameObject)” is equivalent to “if (gameObject != null)”.
Not much difference otherwise. You can only do this with GameObject because Unity has provided an automatic conversion from GameObject to bool that returns true if the GameObject is not null. You can’t do this with any old object in C#, for example a List, unless you write a similar extension. It’s just a nice little shorthand thing.
1 Like
I’m not aware of a practical difference. I just use (gameObject != null) because that’s how I’d write it for pretty much any other kind of object I was doing the same check for. So the code is a little easier to read for me a year later. But that is just personal preference.
1 Like