Why ? ?= not working, but ==null is? (null-coalescing operators)

Hey!
So, this isn’t working:

private void Awake()
{
currentCamera = currentCamera ?? Camera.main;
//or
currentCamera ??= Camera.main;

But

if (currentCamera == null) currentCamera = Camera.main;
//or
currentCamera = currentCamera == null ? Camera.main : currentCamera;

is okay.
Why this happening?
Thanks!

Unity overrides the == operator in UnityEngine.Object. This implementation checks whether an object is compared with null specifically, and if yes, returns true if the object is marked as destroyed, although we’re not actually looking at a null reference, of course.

This seemed like a good idea at the time, but comes with some drawbacks and I’d guess it wouldn’t be implemented this way today.

The ?? operator doesn’t take == operator overrides into account. It literally checks whether we’re looking at a null reference, so Unity’s hacky null check isn’t used here. That’s why it’s not working.

I have a semi-detailed resource on Unity null that goes over this, including some of the other related details.