UNT0009 Missing static constructor with InitializeOnLoad

May 4, 2020 ยท View on GitHub

Provide a static constructor when applying the InitializeOnLoad attribute to a class. This will call it when the editor launches.

Examples of patterns that are flagged by this analyzer

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
class Camera : MonoBehaviour
{
}

Solution

Add static constructor:

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
class Camera : MonoBehaviour
{
    static Camera()
    {
    }
}

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