Changing Cameras With One Button

Hi, I have setup in my game a script which changes the cameras with different buttons, but I want to make it so that I can use the same button to change between cameras, instead of using 2 buttons. How could I solve this problem?

Current Camera Script

public var rearCamera : Camera ;
public var driverCamera : Camera ;


function Start() {
        rearCamera.camera.enabled = true;
        driverCamera.camera.enabled = false;
}

function Update() {

if (Input.GetKey(KeyCode.I))
    {
        rearCamera.camera.enabled = false;
        driverCamera.camera.enabled = true;
    }
	
else if (Input.GetKey(KeyCode.O))
    {
        driverCamera.camera.enabled = false;
        rearCamera.camera.enabled = true;
    }
}

Not tested, but should work… :wink:

public var rearCamera : Camera ;
public var driverCamera : Camera ;
public toggle : boolean = false;

function Start() {
        rearCamera.camera.enabled = true;
        driverCamera.camera.enabled = false;

}

function Update() {

if (Input.GetKey(KeyCode.I) && !toggle)  //<---- enable rear camera
    {
        rearCamera.camera.enabled = true;
        driverCamera.camera.enabled = false;
        toggle = true;
    }

else if (Input.GetKey(KeyCode.I) && toggle)  //<---- enable driver camera
    {
        rearCamera.camera.enabled = false;
        driverCamera.camera.enabled = driver;
        toggle = false;
    }
}

I just ran this and it works pretty well I think.

#pragma strict

public var rearCamera : Camera ;
public var driverCamera : Camera ;


function Start() {
    rearCamera.camera.enabled = true;
    driverCamera.camera.enabled = false;
}

function Update() {
	if(Input.GetButtonDown("CameraSwitch"))
	{
	    rearCamera.camera.enabled = !rearCamera.camera.enabled;
	    driverCamera.camera.enabled = !driverCamera.camera.enabled;
	}
}

I used Input.GetButtonDown, because if you use GetKey it will detect a button press every from that the button is down. So unless you have lightning fingers, or want to swap camera’s every frame, this isn’t ideal. You will need to go to Edit/Project Settings/Input then expand ‘Axes’. Increase size by one. This will copy the last axes. Rename it, and set the ‘Positive Button’ to the button you want to be pressed. In the GetButtonDown function, pass in whatever you named the axes to.

If you need further help, or if this is not the solution you are looking for, let me know.

Try this code. I make this code switching to 2 cameras. `using UnityEngine;
using System.Collections;

public class CameraManagerScr : MonoBehaviour {

%|1051292963_1|%
%|446312010_2|%

%|794809699_3|%

%|1039055902_4|%
%|-552888617_5|%
%|977180863_6|%

%|-934038197_7|%
%|1782251902_8|%
%|-1412472038_9|%
%|-813138912_10|%
%|-21545877_11|%
%|-207334728_12|%
%|837798027_13|%
%|-804862433_14|%

%|270663048_15|%
%|-1892770785_16|%
%|-1459013006_17|%
%|1742211472_18|%

%|-1850636563_19|%
%|-1519131145_20|%
%|1347906305_21|%

%|901506400_22|%
}
`

Your case only works because there is ONLY one object interested in the toggle.
What happens when there are 5 objects interested in the same toggle?

We created a Toggle class that sends events when toggled. Objects interested in it toggling listen for the event. The button OnClick calls the toggle method on the Toggle class. The button does NOT need to know who is interested in the change.