- Home /
Switching between 3 cameras doesn't work
Hello, I have a problem with my code. I don't get why my cameraList.Lenght is equal to 2 and not 3 as I have a size of 3 in the inspector and 3 cameras in my gameobject[] cameraList.
If someone can explain what i'm doing wrong that'd be awesome!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCamera : MonoBehaviour
{
// Start is called before the first frame update
public GameObject[] cameraList;
int currentIndex;
void Start()
{
Debug.Log("cameralenght = " + cameraList.Length);
currentIndex = 0;
cameraList[currentIndex].SetActive(true);
}
// Update is called once per frame
public void Button_Click()
{
Debug.Log("cameralenght2 = " + cameraList.Length);
DesactivateAll();
if (currentIndex < cameraList.Length-1)
{
currentIndex++;
}
else
{
currentIndex = 0;
}
cameraList[currentIndex].SetActive(true);
Debug.Log("Index = " + currentIndex);
}
void DesactivateAll ()
{
foreach (GameObject cam in cameraList)
{
cam.SetActive(false);
}
}
}
Thanks you for your time
Good day. This is a "standard response"
For this kind of problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the "problem". You are the only one who can do it...
You need to debug the code while running, and check the states of all variables at the moment you see the problem, and I'm sure you will detect what is not how it was suposed to be.
If don't know how, look for some tutorials on how to debug code while running.
Bye & good Luck!
If cameraList.Lenght is equal to 2 is because its 2.
Explore why is happening. Debug your code to see its lenght all frames, before each function..
I tried to debug the code and ask for some friends but I didn't get why the problem is happening. I know that people can't help everyone but I just can't get why this is occuring. Sorry ;c
Answer by ATLGAN · Mar 05, 2020 at 11:10 AM
I tried this code. it's working well. Also i edited your code. I added current camera object variable for optimization. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SwitchCamera : MonoBehaviour
{
[SerializeField] GameObject[] m_CameraList;
GameObject m_CurrentCamera;
int m_CurrentIndex;
void Start()
{
//DesactivateAll();
Debug.Log("cameralenght = " + m_CameraList.Length);
m_CurrentIndex = 0;
m_CurrentCamera = m_CameraList[m_CurrentIndex];
m_CurrentCamera.SetActive(true);
//m_CameraList[m_CurrentIndex].SetActive(true);
}
public void Button_Click()
{
Debug.Log("cameralenght2 = " + m_CameraList.Length);
//DesactivateAll();
if(m_CurrentIndex < m_CameraList.Length - 1)
{
m_CurrentIndex++;
}
else
{
m_CurrentIndex = 0;
}
m_CurrentCamera.SetActive(false);
m_CurrentCamera = m_CameraList[m_CurrentIndex];
m_CurrentCamera.SetActive(true);
//m_CameraList[m_CurrentIndex].SetActive(true);
Debug.Log("Index = " + m_CurrentIndex);
}
void DesactivateAll()
{
foreach(GameObject cam in m_CameraList)
{
cam.SetActive(false);
}
}
}
That seems to work fine, I don't get what changed between your code and $$anonymous$$e but I'll take it. Thank you !!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612224407im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera movement - index out of range 1 Answer
Toggle Control between multiple turrets 0 Answers
Array for multiple cameras? 2 Answers
Accessing next Camera from array 1 Answer
How do I change which camera is active based on text dialogue? 2 Answers