Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 808 Bytes

UNT0003.md

File metadata and controls

39 lines (28 loc) · 808 Bytes

UNT0003 Usage of non generic GetComponent

Usage of the generic form of GetComponent, TryGetComponent, GetComponents, GetComponentInChildren, GetComponentsInChildren, GetComponentInParent, and GetComponentsInParent is preferred for type safety.

Examples of patterns that are flagged by this analyzer

using UnityEngine;

class Camera : MonoBehaviour
{
	private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent(typeof(Rigidbody)) as Rigidbody;
    }
}

Solution

Use the generic form of GetComponent:

using UnityEngine;

class Camera : MonoBehaviour
{
	private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
}

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