UNT0030 Calling Destroy or DestroyImmediate on a Transform

July 15, 2022 ยท View on GitHub

Calling Object.Destroy or Object.DestroyImmediate using a Transform argument is not allowed and will produce an error message at runtime.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Camera : MonoBehaviour
{
    public void Update() {
        Destroy(transform);
    }
}

Solution

Destroy the related GameObject instead:

using UnityEngine;

class Camera : MonoBehaviour
{
    public void Update() {
        Destroy(transform.gameObject);
    }
}

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