Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 733 Bytes

UNT0011.md

File metadata and controls

37 lines (26 loc) · 733 Bytes

UNT0011 ScriptableObject instance creation

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.