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?
Answer by CaptainStank · Mar 29, 2020 at 09:19 PM
Yessir / ma'am :) 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 -> https://learn.unity.com/project/c-survival-guide-switch-statements?courseId=5cf06bd1edbc2a58d7fc3209
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;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612225948im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Issue with Rigidbody2D (I think) 2 Answers
How to get depth information from camera and save it per frame? 0 Answers
Why object's doesn't stop on collision enter? 0 Answers
Finding the gameobject closest to the centre of the screen (WorldToViewportPoint) 0 Answers
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers