So tonight I learned that Unity doesn’t actually return null if you call GetComponent<…> or GetComponent(…)
Nope, it returns an object.
An object that if you compare to null will return true… just like if a dead object is compared to null, it too returns true.
BUT
If your object isn’t typed as UnityEngine.Object or some type beneath that… as you know == null doesn’t use the magic unity overload.
Well the same goes if you’re in a generic method that doesn’t constrain to UnityEngine.Object or some type beneath that. The compiler just assumes it’s an object, and uses regular old comparison.
And this is how I learned that sure enough… unity returns an object always when you call GetComponent.
…
It’s just that sometimes… I peak under rocks trying to fix bugs when I find stuff like this. Unity… srsly guys… can we get rid of this garbage, old school, bad design stuff you wrote back in Unity 1. PLEASE!
Fun fact; the bullshit null object is what allows Unity to throw a “MissingReferenceException” instead of “NullReferenceException”. It’s there so Unity can output a helpful message saying “you forgot to assign this in the inspector”.
Fun fact: Unity doesn’t return the bullshit null object in builds. There you only get null.
Yeah, I figured it was something to do with that since all of its members threw exceptions.
Good to know it’s not in build… but then again, that’s just another one of those “oh it acts one way in editor, and another in build… weeeeeee”.
Maybe I was a couple years ago and forgot… but as far as I know, noticed it last night trying to resolve a animation bug I was having in build that was driving me nuts.