Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 656 Bytes

File metadata and controls

37 lines (26 loc) · 656 Bytes

UNT0010 Component instance creation

Use AddComponent() to create components. A component needs to be attached to a GameObject.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Foo : MonoBehaviour { }

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

Solution

Use gameObject.AddComponent():

using UnityEngine;

class Foo : MonoBehaviour { }

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

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