UnityEngine.Object
should not be used with the coalescing assignment operator.
IDE0074 - Use compound assignment
using UnityEngine;
class Camera : MonoBehaviour
{
public Transform a;
public Transform b;
public Transform NC()
{
// Please note that this construct should not be used with Unity objects either, and will trigger a diagnostic.
// We are just using it here for demonstration purposes.
return a ?? (a = b);
}
}
Under normal circumstances, return a ?? (a = b)
can be simplified to return a ??= b
.
Unity has overridden the ==
operator for UnityEngine.Object
. If you use the ??=
operator with those objects, it will not behave as expected because it checks for null in a different way.