This is a bit odd and may end up being a “duh” moment, but for some reason this doesn’t work:
someComponent = gameObject.GetComponent<SomeType>() ?? gameObject.AddComponent<SomeType>();
It gives me an error saying There is no "SomeType" attached to "MyObject"
.
However, if I use this:
someComponent = gameObject.GetComponent<SomeType>() == null ? gameObject.AddComponent<SomeType>() : gameObject.GetComponent<SomeType>();
That works just fine.
I thought the A ?? B
operator meant “if A is not null, use it, otherwise use B”.
I’m not sure what I’m missing, but I don’t get why A == null ? B : A
works but A ?? B
does not.