Skip to main content

Class FindMainCamera

Polls for Camera.main on a configurable retry interval, caches the result, and fires OnMainCameraFound once so all dependents receive the reference without calling Camera.main themselves.

public class FindMainCamera : MonoBehaviour

Inheritance​

Remarks​

Camera.main was historically a scene-wide tag search on every call; Unity has since added internal caching so the cost is lower, but distributing a single cached reference through OnMainCameraFound still avoids repeated lookups across every dependent component and keeps the access pattern consistent.

A retry loop is needed rather than a one-shot Start call because the camera may not be tagged "MainCamera" yet when the scene begins. In XR setups (e.g. a Varjo headset) the XR subsystem spawns and tags the camera asynchronously; in additive multi-scene setups the scene containing the camera may not have finished loading. The configurable interval and attempt count let the retry window be tuned to the slowest expected initialisation path without polling every frame.