Microsoft.Unity.Analyzers/doc/UNT0029.md

679 B

UNT0029 Pattern matching with null on Unity objects

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.