UNT0008 Null propagation on Unity objects

February 5, 2021 ยท View on GitHub

Unity overrides the null comparison operator for Unity objects, which is incompatible with null propagation.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Camera : MonoBehaviour
{
	public Transform NP()
	{
		return transform?.transform;
	}
}

Solution

Use null comparison:

using UnityEngine;

class Camera : MonoBehaviour
{
	public Transform NP()
	{
		return transform != null ? transform : null;
	}
}

A code fix is offered for this diagnostic to automatically apply this change.