Follow

Fun tidbits from the docs on MonoBehaviour:

"This class doesn't support the null-conditional operator (?.) and the null-coalescing operator (??)."

There is a cryptic note that "There is a checkbox for enabling or disabling MonoBehaviour in the Unity Editor. It disables functions when unticked. If none of these functions are present in the script, the Unity Editor does not display the checkbox. The functions are:

Start()
Update()
FixedUpdate()
LateUpdate()
OnGUI()
OnDisable()
OnEnable()"

It does not say where this checkbox is, nor why you would want to do this.

@peterdrake For the same reasons, it does not support the "is null" / "is not null" syntax in newer versions of C#.

@LouisIngenthron @peterdrake the reason for this is that Monobehaviours live in the C# layer AND in the C++ layer of Unity. when you destroy a MB or set it to null the C++ layer gets removed from memory right away while the C# layer has to wait for Garbage Collection. In theory you can have MBs which are null in the C++ world but not in the C# world. Thats why its tricky to do null checks on MBs.

@bernardo_olafson @peterdrake To add to this, if I recall correctly, they'll also "== null" not only when they're actually destroyed, but also when they're queued for destruction (i.e. when you call Destroy() rather than DestroyImmediate()).

But the benefit of all this nonsense is that we can use "== null" as a check of destruction, rather than having to clean up all of our own references to any object we destroy!

@LouisIngenthron @bernardo_olafson My beef (well, one of my beeves) is that this business isn't documented anywhere that is (to me) obvious. It's just scattered around various conflicting and unclear StackOverflow posts, with endless offramps to things like "is {}".

Unity is like Columbo, always turning around at the end and saying, "Oh, and one more thing..."

@peterdrake @LouisIngenthron Unity documentation is quite poor when it comes to more in depth technical info, indeed. you need to collect info from several sources. yes, this can suck...

@peterdrake @LouisIngenthron @bernardo_olafson
The manual pages (rather than scripting reference) is generally better for high level stuff. Pages like this one: docs.unity3d.com/Manual/Execut

The overriding of the null comparison has been an oddity with many unexpected side effects and has been discussed for many many years. It's definitely a doozy :D

@peterdrake Those types of things are typically not really mentioned in the Unity scripting reference docs, you will need to rely on a mix of videos, Unity forum posts, and random social media posts.

If you were curious, that checkbox is at the top of inspector entry for each script. Here's an image of a script with and without one.

@andykorth Oh, THAT checkbox!

I always thought of that as "pretend this component isn't here" rather than "don't call these specific methods", but I guess it amounts to the same thing.

@peterdrake Isn't this just the "enabled" checkbox on a component Inspector window?

It's useful for when you want to debug a scene by reducing the number of active MonoBehaviours, or want to use OnEnable but not through GameObject.SetActive.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.