Avoid using non-generic GetComponent

Rule ND3201 API Usage / Unity

Prefer using the generic version of Component.GetComponent() for type safety.

How to fix

Refactor GetComponent(typeof(MyType)) calls in GetComponent<MyType>().

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Avoid using non-generic GetComponent</Name>
// <Id>ND3201:AvoidUsingNonGenericGetComponent</Id>
warnif count > 0 
from m in Application.Methods where m.IsUsing("UnityEngine.Component.GetComponent(Type)".AllowNoMatch())
select new { 
  m, 
  Debt = 8.ToMinutes().ToDebt(),
  Severity = Severity.Medium
 }

//<Expl>
// The *{0}* is using *UnityEngine.Component.GetComponent(Type)*.
// It should use instead the generic version of *Component.GetComponent()* 
// for type safety.
//</Expl>

//<Desc>
// Prefer using the generic version of *Component.GetComponent()*
// for type safety.
//</Desc>

//<HowToFix>
// Refactor *GetComponent(typeof(MyType))* calls in *GetComponent<MyType>()*.
//</HowToFix>

API Usage / Unity: all 9 items