Skip to content

Avoid usage of GetComponent methods in performance critical context

Vladimir edited this page Dec 7, 2018 · 2 revisions

GetComponent methods allocates memory, so usages of these methods inside performance critical context will lead to regular per frame allocations which will affect performance. Also, some of these methods has expensive CPU runtime cost.

Call in Start and cache results when possible or use direct reference. Alternatively some of these functions like GameObject.GetComponentsInChildren allows for users to provide a list to store results in, this allows for reuse to remove per frame allocations.

Each usage in performance critical context will be highlighted. Context actions for caching result in Start or Awake are available in Alt+Enter

History:

  • This inspection was first added in Rider/ReSharper 2018.3
Clone this wiki locally