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?

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()` 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.

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.

See official docs for more

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.

in Unity 5 I use this:

  1. Goto camera and set a tag to it (“myCamera”) for example
  2. In your code use the following sysntax:

Camera myCamera = GameObject.FindWithTag(“myCamera”).GetComponent<>Camera>();

nJoy

The easiest solution I know:

Camera[] allCameras = FindObjectsOfType<Camera>();

(Of course, this thread is too old for anyone to see this…)

Guys more easy. ( i know that the post is not new.)

if your Camera already is tagged as MainCamera, just do like this:

Camera mycam;
	// Use this for initialization
	void Start () {
		mycam = Camera.main;
	}

[Just to keep post updated]

Another solution is; finding all the camera objects through tag information, and assigning them into an array. Then making changes on the array by using foreach in a function. Example:

public class CreativeObjectController : MonoBehaviour
{
    private GameObject[] frontWallButtons;
    
    private bool _frontWallDisabled;

    private void Start()
    {
        frontWallButtons = GameObject.FindGameObjectsWithTag("FrontWallButtons");

        _frontWallDisabled = true;

    }

    private void Update()
    {
        // If 0 is pressed close the boxes front wall and front wall buttons
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            _frontWallDisabled = !_frontWallDisabled;
            FrontWallButtonsController(_frontWallDisabled);
        }
    }

    private void FrontWallButtonsController(bool state)
    {
        foreach (GameObject button in frontWallButtons)
        {
            button.SetActive(state);
        }
    }
}

One thing not covered by some of the “FindObjectsOfType” or “Camera.Main” answers is that you may have multiple cameras in your scene, some of which may be disabled at any given time. In this case, you can do something like:

Camera[] allCameras = FindObjectsOfType<Camera>( includeInactive: true );

One thing that I noticed is that when you delete the main camera and add a new one, “Camera.main.” will return nothing unless you give the new camera the tag “MainCamera”.
I thought Unity would figure that out by itself (perhaps I misread the docs), but seemingly it expects us to set it ourselves.