Help with camera switch

I know this should be a fairly easy script, but it’s giving me massive problems (new to scripting, hinthint)

This is my script:

var mainCamera : Camera; //Main Camera

var overheadCamera : Camera; // Camera I want to switch too

mainCamera = GameObject.Find("Main Camera"); //Find my main camera
overheadCamera = GameObject.Find("OverHead Camera"); //Find my overhead camera

function Update () {
 if(Input.GetKeyDown ("c")) //if I press c, switch to my overhead camera
      { 
        mainCamera.enabled = false;
        
        overheadCamera.enabled = true;
    
        
     }
if(Input.GetKeyUp ("c")) //if i release c, switch to my main camera
      { 
        mainCamera.enabled = true;
        
        overheadCamera.enabled = false;     

}
}

It gives me the error: Assets/Scripts/Camera Switch.js(11,33): BCE0022: Cannot convert ‘UnityEngine.GameObject’ to ‘UnityEngine.Camera’.

So it dislikes Game.Object and .Camera being used? I tried both routs of only going game.object’s and just .camera’s, but both end in more errors…What am I doing wrong?

At the top of your script, declare your cameras as GameObjects. Then add a Start function and put this part inside it:

mainCamera = GameObject.Find(“Main Camera”); //Find my main camera
overheadCamera = GameObject.Find(“OverHead Camera”); //Find my overhead camera

See if that works.

Like This?

#pragma strict

var mainCamera : GameObject;
var overheadCamera : GameObject;

function Start() {


var mainCamera : GameObject;

var overheadCamera : GameObject;

mainCamera = GameObject.Find("Main Camera");
overheadCamera = GameObject.Find("OverHead Camera");

}


function Update() {
 if(Input.GetKeyDown ("c"))
      { 
        mainCamera.enabled = false;
        
        overheadCamera.enabled = true;
    
        
     }
if(Input.GetKeyUp ("c"))
      { 
        mainCamera.enabled = true;
        
        overheadCamera.enabled = false;     

}
}

This gave me almost the same error: ‘enabled’ is not a member of ‘UnityEngine.GameObject’.

Ok, try using .active instead of .enabled and remove the var declarations from the Start function, but leave evyerything else as is.

Awesome! The .enabled is what was throwing it off, thanks so much!

Edit- It seems GetKeyUp is non functional though…it changes for when I press c, but it’s bugged stuck even after I release…

Double edit- Fail on my part, I just didn’t attach the script to the overhead camera. Script works fantastic, thanks again