How to change camera when mouse button is clicked?

I have multiple camera attached to separate empty game objects that surround the player. I want the camera to switch in order when the user presses the middle mouse button. However unity is not recognizing any of the objects in my scene. I’m guess I am forgetting to call something. Any help would be great!

//Player is the gameObject which holds the camera
Player.camera.active = false;
var CameraNum : int;
CameraNum = 0;
 

function Update ()
{
  if(Input.GetMouseButtonDown(2))
  	{
  	  	CameraNum+=1;
  	  	Debug.Log ("You hit middle mouse button");
  	}
  if(CameraNum == 1)
  	{
  		//CameraOne is an empty game object that is paired with a camera
  		CameraOne.camera.active = true;
  		Debug.Log ("Camera One active");
  	}
  if(CameraNum == 2)
  	{
  		//CameraTwo is an empty game object that is paired with a camera
  		CameraOne.camera.active = false;
  		CameraTwo.camera.active = true;
  		Debug.Log ("Camera Two active");
  		
  	}
  if(CameraNum == 3)
  	{
  		//CameraThree is an empty game object that is paired with a camera
  		CameraTwo.camera.active = false;
  		CameraThree.camera.active = true;
  		Debug.Log ("Camera Three active");
  	}
}

Have you considered using a single camera, and applying new positions/rotations on each MMB click?

Modify your inputs to be for example “MiddleMouse” to be the middle mouse button by going into Edit > Project Settings > Input, and then modify all your inputs. Then once you have done that you can just replace if(Input.GetMouseButtonDown(2)) to if(Input.GetButtonDown(“”)) That would make things a bit easier. And also change the active term to enabled = true or = false depending on what you would like to activate/deactivate. I hope this helps :slight_smile: