Simple Camera Switch Using C#

Hello, I’m new to the land of Unity and I’m trying to muddle my way through. I’m probably having issues due to inexperience using Unity and C#.

In very simple scene, I’m trying to make 2 camera’s switch when a key is pressed on the board (C). Is there a simple method to achieve this using enabled = true?

Other notes that could be of use:

I have followed other vid tuts without success. :frowning:
I have nested 2 camera’s in a a Empty Game Object… do I need to do this?
I’m a newb.

Thanks for you help.

You can group your cameras, but don’t have to.

Regarding the switch, you want to make sure you activate one and not the other.
Something in the like :

public Camera camOne;
public Camera camTwo;

private void Awake ()
{
    camTwo.enabled = false;
}

public void SwitchCam ()
{
    camOne.enabled = !camOne.enabled;
    camTwo.enabled = !camTwo.enabled;
}

This is one of many ways to do it.

Morning… Managed to stop it spitting back errors but the camera transitions doesn’t work. Another thing to note is that the object in my scene is flash/blinking rapidly. Any clues?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraSwitch : MonoBehaviour {

public Camera camera1;
public Camera camera2;

private void Awake ()
{
	camera2.enabled = false;
}

public void SwitchCam ()
{
	camera1.enabled = !camera1.enabled;
	camera2.enabled = !camera2.enabled;
}
	

	void Update() {
	if (Input.GetKeyDown(KeyCode.C))
			print("C key was pressed");
		SwitchCam ();

	}

}

Thanks,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Manycameras : MonoBehaviour {

	public Camera camera1;
	public Camera camera2;
	public Keycode key;

	private void Awake ()
	{
		camera2.enabled = false;
	}

	public void SwitchCam ()
	{
		camera1.enabled = !camera1.enabled;
		camera2.enabled = !camera2.enabled;
	}
}

void Update()
{
	if (Input.GetKeyDown (KeyCode.C))
	{
		Debug.Log ("C key was pressed");
		SwitchCam ();
	}
}

Well I tried this code as I wish for more than one camera in my game I want lots as I want to be able to see from both friendlies and enemies but all I keep getting when I run this code is this

Blockquote
: Error CS0116: A namespace cannot directly contain members such as fields or methods (CS0116) (Assembly-CSharp)