- Home /
Finding Cameras
How would you find a camera? GameObject.Find does not work because that only finds GameObjects. Is there a similar function for cameras? If there isn't, is there another way to get a reference to a camera via script?
Answer by Peter G · Apr 22, 2010 at 11:32 PM
To find the main camera, you can use Camera.main. Or to find an array of all the cameras in your scene, use Camera.allCameras.
Honestly though, I would still use GameObject.Find() and add .GetComponent<Camera>()
to the end.
// C#
Camera cam = GameObject.Find("myObject").GetComponent<Camera>();
That way you can still find the camera by using Find() which is easier than finding it in an array.
Ah. Couldn't find where to do that anywhere. Thanks a lot.
GameObject.Find() is slow! Even GameObject.FindWithTag() seems a little much here. Be my guest, but Camera.main is just as easy and much faster. If you have multiple cameras just look through the array till you find one whose gameObject's name identifies it as the one you want.
As Peter commented in the other answer. Its is not meant to run every frame. You find the camera on Start() only.
Answer by DtBeloBrown · Apr 23, 2010 at 01:41 AM
You have many options. Here are some, roughly in order of execution speed:
1) Simply drag a reference of the desired camera to a variable of type Camera
in your script.
2) use Camera.main
if the camera you want is the only active one right now.
3) If you have multiple cameras named uniquely, check foreach (Camera c in Camera.allCameras)
and if
one, c
, whose .gameObject.name == "DesiredCamera"
then that is the camera you want.
I do not recommend These next two options unless you need them. They are considerably slower and should not be done every frame:
4) GameObject.FindWithTag("Main Camera").camera
5) GameObject.Find("Main Camera").camera
is slowest, don't use it unless you really have to.
Your numbers 4 and 5 are not supposed to be run every frame. The idea is to cache the object at the Start(). So you are correct that they are performance heavy, but in this case it would not make a difference because you are only calling the look up on the first frame.
Is there an appreciable difference in speed between 3 and 5 if I run them only on Start()?
Answer by Molix · Apr 22, 2010 at 11:29 PM
Camera.allCameras is an array of all active cameras. However, depending on the usage, you may just want to have your camera find you---e.g. tell you about itself when it Starts()---or find it once when you Start(), or put a reference member to the GameObject/Transform that contains it.
Answer by li3ro · Jul 11, 2015 at 12:26 PM
in Unity 5 I use this:
Goto camera and set a tag to it ("myCamera") for example
In your code use the following sysntax:
Camera myCamera = GameObject.FindWithTag("myCamera").GetComponent<>Camera>();
nJoy
Answer by Harinezumi · Mar 16, 2018 at 09:26 AM
The easiest solution I know:
Camera[] allCameras = FindObjectsOfType<Camera>();
(Of course, this thread is too old for anyone to see this...)