Switching between multiple cameras created in runtime

I’m placing a bunch of prefabs on a terrain at the position of a mouse click. Each of these prefabs has a camera object. How can I switch between them?

When I run my code it says that there is no camera object attached to my “Turret” prefab. But I have a camera as a child object to the turret parent. I guess I don’t know how to attach a camera to a gameobject. Even if I had that part working I’m not sure my code would work anyway.

As a secondary question, is there a way to keep cameras enabled and switch between them or do you have to only have one enabled at a time?

My current code looks like this:

using UnityEngine;
using System.Collections;

public class ChangeCamera : MonoBehaviour 
{
	Camera activeCamera;
	float minDistance = 0;
	Camera[] cameras = new Camera[10];
	GameObject[] gos;
	int curCam = 0;
	
	void Update () 
	{
		if (Input.GetButtonDown("Fire3"))
		{			
			gos = GameObject.FindGameObjectsWithTag("Turret");
			
			for(int i = 0; i < gos.Length; i++)
			{
				cameras _= gos*.camera;*_

* }*

* Debug.Log(cameras.Length);*

* activeCamera = Camera.current;*
* activeCamera.enabled = false;*
* activeCamera = cameras[curCam];*
* activeCamera.enabled = true;*

* curCam++;*
* if (curCam > cameras.Length)*
* {*
* curCam = 0;*
* }*

* Debug.Log(activeCamera.ToString()); *
* }*

* if (Input.GetButtonDown(“Fire4”))*
* {*
* if(activeCamera != Camera.main)*
* {*
* activeCamera = Camera.current;*
* activeCamera.enabled = false;*
* activeCamera = Camera.main;*
* activeCamera.enabled = true;*
* }*
* Debug.Log(activeCamera.ToString());*
* }*
* }*
}
Thanks!

Instead of having lots of cameras, you should just have the one camera and move it around a lot! Have lots of empty transforms set up the way you want them, and then do something like this when you want to switch to a particular ‘angle’:

Camera.main.transform.parent = newCameraAngleTransform;
Camera.main.transform.localPosition = Vector3.zero;
Camera.main.transform.localRotation = Quaternion.identity;

If you wanted to have more settings than just position and rotation, you can put a disabled camera on each ‘camera angle’, and then use

Camera.main.CopyFrom(newCameraAngleTransform.camera);
Camera.main.enabled = true;

I played around (for a few hours) and finally have something that works the way I want it to.

using UnityEngine;
using System.Collections;

public class ChangeCamera : MonoBehaviour 
{
	Camera activeCamera;
	ArrayList cameras = new ArrayList();
	int camCount;
	int camIndex;
	
	void Update() 
	{
		if (Input.GetButtonDown("Fire3"))
		{			
			foreach (Camera cam in Camera.FindSceneObjectsOfType(typeof(Camera)))
			{				
				cameras.Add(cam);
			}
						
			if (camCount < cameras.Count)
			{
				camIndex = 1;
			}
			
			camCount = cameras.Count;
			
			activeCamera = Camera.current;
			activeCamera.enabled = false;
			activeCamera = cameras[camIndex] as Camera;
			activeCamera.enabled = true;
			
			Debug.Log("Current camera at index: " + camIndex);
			Debug.Log(activeCamera.ToString());			
			Debug.Log("Total cameras: " + cameras.Count);
			
			camIndex++;
			if (camIndex > cameras.Count-1)
			{
				camIndex = 0;
			}
		}
		
		if (Input.GetButtonDown("Fire4"))
		{
			foreach (Camera cam in Camera.FindSceneObjectsOfType(typeof(Camera)))
			{				
				cameras.Add(cam);
			}
			
			if(activeCamera != Camera.main)
			{
				activeCamera = Camera.current;
				activeCamera.enabled = false;
				activeCamera = cameras[cameras.Count-1] as Camera;
				activeCamera.enabled = true;
			}
			Debug.Log(activeCamera.ToString());
		}
		
		cameras.RemoveRange(0, cameras.Count);
	}
}