I'm a newbie and i am having a problem with some code,Im a newbie in unity and i dont know whi this code is not working

basically i wrote this code for switching cameras in a project:

private GameObject camera1;
private GameObject camera2;
private int numeroCamera;
// Start is called before the first frame update
void Start()
{
    numeroCamera=0;
    camera1=GameObject.Find("Main Camera");
    camera2=GameObject.Find("Camera");
    camera1.gameObject.SetActive(true);
    camera2.gameObject.SetActive(false);
}

// Update is called once per frame
void Update()
{
  if(Input.GetKeyDown(KeyCode.C))
  {
    numeroCamera+=1;
    if(numeroCamera>1)
    {
      numeroCamera=0;
      Debug.Log("reset");
    }
    if(numeroCamera==0){
      camera1.gameObject.SetActive(true);
      camera2.gameObject.SetActive(false);
      Debug.Log("0");
    }
    if(numeroCamera==1){
      camera1.gameObject.SetActive(false);
      camera2.gameObject.SetActive(true);
      Debug.Log("1");
    }
  }
}

}
the problem is that when i press c for the first time, the code works well but the other times it doesn’t switch cameras, can somebody help me?,basically im doing a cam switch script. for now i wrote this:

private GameObject camera1;
private GameObject camera2;
private int numeroCamera;
// Start is called before the first frame update
void Start()
{
    numeroCamera=0;
    camera1=GameObject.Find("Main Camera");
    camera2=GameObject.Find("Camera");
    camera1.gameObject.SetActive(true);
    camera2.gameObject.SetActive(false);
}

// Update is called once per frame
void Update()
{
  if(Input.GetKeyDown(KeyCode.C))
  {
    numeroCamera+=1;
    if(numeroCamera>1)
    {
      numeroCamera=0;
      Debug.Log("reset");
    }
    if(numeroCamera==0){
      camera1.gameObject.SetActive(true);
      camera2.gameObject.SetActive(false);
      Debug.Log("0");
    }
    if(numeroCamera==1){
      camera1.gameObject.SetActive(false);
      camera2.gameObject.SetActive(true);
      Debug.Log("1");
    }
  }
}

}

basically the first time i switch cam with c it works, other times it doesn’t work, can you help me?

Yessir / ma’am :slight_smile:
First and foremost, I am going to assume you put both cameras in an Empty GameObject (just to make the hierarchy look clean) and attached the script to the Empty GameObject. Next, (in the script) you want to make the Camera GameObjects public because the script won’t know what GameObject you are referencing unless you do that. Then, you drag the cameras into the GameObject slots. If you already did this or it sounds like I’m belittling you then I promise I’m not, but just making sure as the script won’t work unless you’ve done it.

In the Script:
So what’s happening is you’re constantly adding to numeroCamera in Update() which is fine but you have the if statements inside of the GetKeyDown check. So every time you’re pressing “C” the value of numeroCamera is increasing by one, meaning it works! But you have the if statements that check the value of numeroCamera running on a kind of loop. You press “C” once, and it is equal to 1. You press it again, and the value is equal to 2. Now, it is greater than one, so the value resets, but you the value is still going to increase because you pressed the button (so now numeroCamera = 1). You press it again, and the value is greater than one, it resets, and the camera doesn’t change. To solve, you can leave the numeroCam += 1 (or numeroCam++ which is the same thing) inside (Input.GetKeyDown(KeyCode.C)) but take everything else out and just leave it in Update().

P.S. I would suggest you use switch statements instead of using multiple if-else statements. Learn it for free from this Unity course → C# Survival Guide - Switch Statements - Unity Learn

Code:
using UnityEngine;

public class CameraScript : MonoBehaviour
{
public GameObject camera1;
public GameObject camera2;
private int numeroCamera;

void Start()
{
    numeroCamera = 0;
    camera1.SetActive(true);
    camera2.SetActive(false);
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.C))
    {
        numeroCamera++; // or numeroCamera += 1

        /*Your Code:
         
          numeroCamera += 1;
        if (numeroCamera > 1)
        {
            numeroCamera = 0;
            Debug.Log("reset");
        }
        if (numeroCamera == 0)
        {
            camera1.gameObject.SetActive(true);
            camera2.gameObject.SetActive(false);
            Debug.Log("0");
        }
        if (numeroCamera == 1)
        {
            camera1.gameObject.SetActive(false);
            camera2.gameObject.SetActive(true);
            Debug.Log("1");
        }*/
    }

    switch (numeroCamera)
    {
        case 0:
            camera1.SetActive(true);
            camera2.SetActive(false);
            Debug.Log("1");
            break;

        case 1:
            camera1.SetActive(false);
            camera2.SetActive(true);
            Debug.Log("2");
            break;

        case 2:
            numeroCamera = 0;
            Debug.Log("Reset");
            break;
        }
}

}