UNT0011 ScriptableObject instance creation

December 11, 2019 ยท View on GitHub

Use CreateInstance() to create a ScriptableObject. To handle Unity message methods, the Unity engine needs to create the ScriptableObject.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Foo : ScriptableObject { }

class Camera : MonoBehaviour
{
    public void Update() {
        Foo foo = new Foo();
    }
}

Solution

Use ScriptableObject.CreateInstance():

using UnityEngine;

class Foo : ScriptableObject { }

class Camera : MonoBehaviour
{
    public void Update() {
        Foo foo = ScriptableObject.CreateInstance<Foo>();
    }
}

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