UNT0018 : System.Reflection features in performance critical messages
July 24, 2020 ยท View on GitHub
You should not use System.Reflection features in performance critical messages like Update, FixedUpdate, LateUpdate, or OnGUI.
Reflection can be slow and cause lags.
Examples of patterns that are flagged by this analyzer
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
void Update()
{
var field = Type.GetType("foo").GetField("bar");
// ...
}
}
Solution
If you must use System.Reflection to access a private member, consider caching results in Start() or Awake(), and use the cache in the loop instead.
In many cases, alternatives are available which don't require reflection. For example, if a game object has a private field referencing a UI component, a direct reference to the component can be obtained using code such as FindObjectOfType<SomeGO>().GetComponent<T>(); the component can be cached and subsequently directly accessed in the update loop.