UNT0029 Pattern matching with null on Unity objects

June 29, 2022 ยท View on GitHub

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

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Camera : MonoBehaviour
{
    public Transform a = null;

    public void Update()
    {
        if (a is not null) { }
    }
}

Solution

Use null comparison:

using UnityEngine;

class Camera : MonoBehaviour
{
    public Transform a = null;

    public void Update()
    {
        if (a != null) { }
    }
}

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