V3213. Unity Engine. The 'GetComponent' method must be instantiated with a type that inherits from 'UnityEngine.Component'.
The analyzer has detected that the argument to the GetComponent
method (or similar) of the Component
or GameObject
classes is passed as a type that neither inherits from UnityEngine.Component
nor is an interface. In such cases, the method always returns null
or throws an exception. The use of these methods in this way must be avoided.
Look at the example:
using UnityEngine;
class Camera : MonoBehaviour
{
private int num;
private void Start()
{
num = GetComponent<int>();
}
}
The int
base type is used as an argument of the GetComponent
method. The method call always throws an exception.
The example with the typeof(int)
argument:
using UnityEngine;
class Camera : MonoBehaviour
{
private int num;
private void Start()
{
Component foundComponent = GetComponent(typeof(int));
}
}
The GetComponent
method with the typeof(int)
argument always leads to throwing an exception.
In such cases, you can refactor the code to remove an incorrect method call.
This diagnostic is classified as: