Switch camera with mouse

hi,

im currently using this to switch cameras

var cam1 : Camera;

var cam2 : Camera;

function Update () {

     if(Input.GetKeyDown("1")){

          cam1.enabled = true;

          cam2.enabled = false;
     }
     if(Input.GetKeyDown("2")){

          cam1.enabled = false;

          cam2.enabled = true;
     }
}

But instead of pressing a button, i want to change camera when i click on an object with the left mouse button

Try OnMouseDown on your object scripts.

In the camera script:

var helperObject: GameObject; // assign this in the inspector

function OnMouseDown () {
    helperObject.SwitchCam(1); // or 2 or 3... whatever the number
}

On your helper object's script:

var cam1:Camera;  // assign these in inspector
var cam2:Camera;
var cam3:Camera;

function SwitchCam(newCam:int) {
    cam1.enabled = false;
    cam2.enabled = false;
    cam3.enabled = false;
    switch (newCam) {
        case 2:
            cam2.enabled = true;
        break;
        case 3:
            cam3.enabled = true;
        break;
        default:
            cam1.enabled = true;
        break;
    }
}

Now, there are other good coding practices (like using enums instead of numbers, etc) that you can include, but this should get you started.

can i insert the function OnMouseDown in the camera script? I want to remove the second button and then give every object his own script. Something like:

Object 1:

var cam1 : Camera;

var cam2 : Camera;

function Update () {

if(Input.GetKeyDown("1")){ >>>>Now here the function OnMouseDown () {

  cam1.enabled = true;

  cam2.enabled = false;

}

Object 2:

var cam1 : Camera;

var cam2 : Camera;

function Update () {

if(Input.GetKeyDown("1")){ >>>>Now here the function OnMouseDown () {

  cam1.enabled = false;

  cam2.enabled = true;

}