Switch Camera on Input in C#

#pragma

using UnityEngine;
using System.Collections;

public class MenuPrototypes : MonoBehaviour {

bool cam = true;
bool cam2 = false;

void Start ()
{
	bool cam = true;
	bool cam2 = false;

I need help with switching from one camera to another by pressing ‘Enter.’ Here is my code. All the examples I see are in JavaScript but not C# : /. Any help is truly appreciated. I have been trying to figure this out for three days.

Please be as specific as possible as I am not good at scripting… obviously. }

void SwapCamera ()
{
	if (Input.GetKeyUp(KeyCode.Return))	{			
	bool cam = false;
	bool cam2 = true; 
}

	}

}

You are declaring the cam and cam2 variables locally, by using the bool type each time. Once you declare a variable, like you did at the top, you reference it without the type, i.e. cam = true;

I’ll assume you have not referenced any camera’s. Add these two lines, replacing cam and cam2 variables at the top:

public Camera camera;
public Camera camera2;

Then change your start routine:

void Start() {
camera.enabled = true;
camera2.enabled = false;
}

void Update() {
//This will toggle the enabled state of the two cameras between true and false each time
if (Input.GetKeyUp(KeyCode.Return)) {
camera.enabled = !camera.enabled;
camera2.enabled = !camera2.enabled;
}

Add your code to a GameObject, NOT the camera’s. Attach your camera’s to Camera and Camera2 against this GameObject in the inspector panel. Run the code and it should switch between the 2 camera’s. Setup a scene so you can see it switch. This hasn’t been tested, I wrote it straight in here but it should work.

This works but I can see my body in the second camera, it looks like I’m teleporting

For any developer who reads this - camera change does not work after exporting to an exe-file

Thanks a lot, good idea, but I have some problems with this so I change it to use SetActive and change it to use any number of cameras or any kind objects:

using UnityEngine;

public class ChangeObject : MonoBehaviour
{

	public GameObject[] objects;
	private int objectActual = 0;

	void Start()
	{
		DisableObjects();
		objects[0].gameObject.SetActive(true);
	}

	void Update()
	{
		if (Input.GetKeyUp(KeyCode.Return))
		{
			DisableObjects();
			objectActual++;
			if (objectActual >= objects.Length)
			{
				objectActual = 0;
			}
			objects[objectActual].gameObject.SetActive(true);
		}
	}

	void DisableObjects()
	{
		foreach (GameObject obj in objects)
		{
			obj.gameObject.SetActive(false);
		}
	}
}

I hope it could be usefull