Cannot switch cameras!!! Help ASAP!!!

Hi! I need to be able to switch between 4 cameras in my game. Here is my code:

var cam1 : Camera;
var cam2 : Camera;
var cam3 : Camera;
var cam4 : Camera;

function Start () {
 cam1.enabled = true;
          cam2.enabled = false;
          cam3.enabled = false;
          cam4.enabled = false;      
}



function Update () {
 if(Input.GetKeyDown(KeyCode.A)){
          cam1.enabled = true;
          cam2.enabled = false;
          cam3.enabled = false;
          cam4.enabled = false;
          }
          
          if(Input.GetKeyDown(KeyCode.S)){
          cam1.enabled = false;
          cam2.enabled = true;
          cam3.enabled = false;
          cam4.enabled = false;
          }
          
          if(Input.GetKeyDown(KeyCode.D)){
          cam1.enabled = false;
          cam2.enabled = false;
          cam3.enabled = true;
          cam4.enabled = false;
          }
          
          if(Input.GetKeyDown(KeyCode.F)){
          cam1.enabled = false;
          cam2.enabled = false;
          cam3.enabled = false;
          cam4.enabled = true;
          }
}

This does change the cameras, but after i stop playing the game! How can I have it so the change happens during the game? I have assigned the four cameras to the variables in the Unity GUI, but still nothing. The cameras are part of an object and are in the assets.
I need help, my project is due Friday.

You have linked the camera instances of your prefab in those variables and you’re probably instantiating that prefab at runtime. Now you enable / disable the cameras in your prefab but that won’t change the actual game instance.

There are some ways:

  • Either have the object with the cameras already in the scene and link the for cameras to those instances
  • or if you need to instantiate the object at runtime, you have to setup the camera references manually through scripting.
  • Attach that script to the prefab itself so when the object is instantiated the prefab links are automatically “translated” to the new instances of the clone.

You can’t reference the cameras on the prefan and expect that the cameras of an instance of that prefab would change at runtime since an instance is just a clone / copy of the prefab,