UNT0010 Component instance creation
September 17, 2020 ยท View on GitHub
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.