It’s not so much a bug as it is a result of a kind of bad decision by the Unity team early on in development, Unity overrides .Equals on any variables that can show up in Inspector, it’s a fake null state that isn’t read by the ?? operator.
If you set that variable to be private, you’ll see that the operator suddenly works.
using UnityEngine;
public class TestOperator : MonoBehaviour {
private GameObject privateObj;
public GameObject publicObj;
public GameObject privateObjReference;
void Start() {
privateObj = privateObj ?? new GameObject("Private Object");
publicObj = publicObj ?? new GameObject("Public Object");
privateObjReference = privateObj; //So we can easily see if the private object was actually assigned
}
}
Works for me as I’d expect in this situation and creates a new object.
It’s a little different when there’s the chance to run into the special null-scenario, which @Invertex mentioned.
But in this case there’s no apparent situation where this could apply, since GameObject.Find just returns ‘null’ if it didn’t find anything (unless this has just recently been changed).
Having this exceptional case though is simply an indicator that this operator should be used either with caution or should be avoided completely in Unity. Of course, if you know your reference type will never have the chance to be a UnityEngine.Object, you can go for it… But all the evil can already start with interfaces, generics etc.