V3211. Unity Engine. The operators '?.', '??' and '??=' do not correctly handle destroyed objects derived from 'UnityEngine.Object'.
The analyzer has detected a potential error related to the object derived from UnityEngine.Object. This object was used with the null-conditional (?.) or null-coalescing (?? and ??= ) operator.
According to the Unity documentation, it is not recommended to use the ?., ??, and ??= operators. They cannot be overloaded, so they do not consider that objects derived from UnityEngine.Object may be destroyed. Therefore, the check may have an incorrect result.
Look at the example:
class CustomComponent: MonoBehaviour
{
public bool HitConfirm(....)
{
Projectile bullet;
....
if(....)
Destroy(bullet);
....
if(bullet?.isJammed())
{....}
}
}
When addressing bullet, the check with a null-conditional operator (?. ) will not consider that the object may be destroyed, since this operator cannot be overloaded.
To fix this, it is recommended to use a check with the == or != operator.
The fixed code:
class CustomComponent: MonoBehaviour
{
public bool HitConfirm(....)
{
Projectile bullet;
....
if(....)
Destroy(bullet);
....
if(bullet != null ? bullet.isJammed() : null)
{....}
}
}