- Home /
Camera.main look up
How exactly unity API store Camera.main? I know gameobject with a tag MainCamera is looked up and returned. But, does that mean that every time i get the property a gameobject find by tag is executed, or it's cached somehow more efficiently to save unnecessary tag search?
Answer by zach-r-d · Jul 28, 2015 at 10:14 PM
Can't be 100% certain without access to the Unity source, but I would bet money that Camera.main is just a convenience property that wraps finds by tag and calls it every time.
Game object tags can be changed at runtime (meaning that the "main camera" object can theoretically change moment-to-moment), so caching the value would require listening for tag changes on all game objects. Even if there were an API for that, it would be a performance imposition on any code that changes game object tags, which is unreasonable when callers of Camera.main can just cache the return value.
Yeh, that's what i was thinking. But there might be certain info check on each scene gameobject instantiation and destroy on unity side. So technically its possible without event driven behaviors, but i guess im safer to use my own cached instance and not relay on camera.main.
The tag of an already existing object can be changed (as simple as gameObject.tag = "$$anonymous$$ainCamera"), so even checking on instantiation/destruction wouldn't help.
It is indeed possible without event driven behaviors (it could theoretically be hard coded into the GameObject.tag property setter), but that would be pretty absurd.
Agree, sad that docs don't mention this, personally i think it should be a must if such simple operation are executed in such hardcore way. If you have couple scripts running camera.main in couple updates, its really an optimization sin against humanity! and devs should know!
Your answer
Follow this Question
Related Questions
How do I find the closest target with a tag? c# 2 Answers
Main Camera flys up on "play" 0 Answers
Rotation using click and ctrl script?! 0 Answers
find GUIText 1 Answer
FindGameObjectWith "next" Tag 2 Answers