?? operator problem

Hi, I use ?? operator. ?? and ??= operators - null-coalescing operators | Microsoft Learn

_rigidbody=GetComponent<Rigidbody>() ?? gameObject.AddComponent<Rigidbody>();

I use that code to get the current rigibody component, if it is not exist, add it. So the _rigidbody suppose to not null, but the _rigibody is null. Can anyone help me, please?

I have never been able to solve this problem as well. You will have to do this instead I guess :

 _rigidbody=GetComponent<Rigidbody>() ;
if( _rigidbody == null ) _rigidbody = gameObject.AddComponent<Rigidbody>();

Or maybe this (haven’t tested it) :

 _rigidbody= ( (_rigidbody = GetComponent<Rigidbody>() ) != null ) ? _rigidbody : gameObject.AddComponent<Rigidbody>();