UNT0015 Incorrect method signature with InitializeOnLoadMethod, RuntimeInitializeOnLoadMethod or DidReloadScripts attribute
March 22, 2022 ยท View on GitHub
Unity needs a method decorated with InitializeOnLoadMethod, RuntimeInitializeOnLoadMethod or DidReloadScripts attribute to be static and parameterless. Otherwise either Unity won't call it or will throw NullReferenceException.
Examples of patterns that are flagged by this analyzer
using UnityEditor;
class Loader
{
[InitializeOnLoadMethod]
private void OnLoad(int foo, string bar) {
}
}
Solution
Fix method signature:
using UnityEditor;
class Loader
{
[InitializeOnLoadMethod]
private static void OnLoad() {
}
}
A code fix is offered for this diagnostic to automatically apply this change.