Camera.main is slow


  • I use Camera.main many times in scripts and I want to optimize my game performance.


Camera.main takes more time to execute than you might expect. If you use it many times per game loop, you will be wasting time unnecessarily.

Below you can see the cost reported by the profiler Camer.main (Camera.get_main() in the profiler) when being called 100000 times in this extreme example.


You should store a reference to each of your Cameras once in the lifetime of that Camera, and then use that stored reference in the rest of the code. For example:

// Initialization code for scene

Camera mainCamera = Camera.main;

// Game code, executed once per frame or more

Vector3 pos = mainCamera.transform.position;

Below is the same test as above but using the Camera MainCamera = Camera.main reference instead of calling Camer.main. You can see the time spent in Update() is considerably lower.

More Information

Camera.main returns the first Camera in the scene tagged "MainCamera", so it may be slower if you have additional Cameras that are tagged differently. Even for a single Camera, the tag checking means it will always be slower than a direct reference.

Script Reference page:


Was this article helpful?
3 out of 3 found this helpful
Have more questions? Submit a request


  • 0

    I had a feeling about this, I was 99% sure that it doesn't matter which one should i use, but i was wrong. I'm glad i know that now thanks.

    I'm crazy about performance hahaha.

    Edited by Dannark
  • 0
    Pablo Duarte

    Thank you, I wasn't sure about how would I "Cache" the camera, from the suggestion on the Unity Docs for the Camera.Main, and I still not 100% sure about the stuff I've been doing but this article of yours helped me out, thanks again.

Please sign in to leave a comment.