Skip to content

Camera.main is inefficient in frequently called methods

Matt Ellis edited this page Feb 19, 2019 · 15 revisions

The Camera.main property returns the first camera that is tagged with "MainCamera". Internally, it does this by calling FindGameObjectsWithTag, and doesn't cache the result, so accessing this property frequently will have an impact in performance. Consider caching the camera in a field, and setting value in Start or Awake.

This inspection will add a performance indicator highlight to the Camera.main property when it is accessed in a performance critical context. The inpection also provide context actions via Alt+Enter to cache the result in Awake or Start method.

See the documentation for Camera.main and also the support document "Camera.main is slow".

History:

  • This inspection was converted into a performance indicator in Rider/ReSharper 2018.3
  • This inspection was first added in Rider/ReSharper 2018.2
Clone this wiki locally