- Home /
 
Accessing next Camera from array
Hello, I'm doing camera swap so when Player press "C" current camera will be disabled and next camera from the array will be enabled. I know that i can't use that number to pick array element but I don't know how to do it other way. I'm not really good with arrays so I'm asking for advice. Here is part of the code:
     GameObject[] availableSpecCameras;
             int number;
             GameObject mainPlayerCamera;
         GameObject nextCamera;
         GameObject lastCamera;
             
             void Start() {
             mainPlayerCamera = GameObject.FindGameObjectWithTag("MainCamera");
             availableSpecCameras = GameObject.FindGameObjectsWithTag("SpecCamera");
             number = availableSpecCameras.Lenght;
             }
             
             void Update () {
             if (Input.GetKeyDown (KeyCode.C)) {
                         if (mainPlayerCamera.gameObject.activeInHierarchy) {
                             nextCamera = availableSpecCameras [number];
                             nextCamera.gameObject.SetActive (true);
                             mainPlayerCamera.gameObject.SetActive (false);
                                 number--;
                         } else if (number != 0) {
                             nextCamera = availableSpecCameras [number];
                             nextCamera.gameObject.SetActive (true);
                             lastCamera = availableSpecCameras [number + 1];
                             lastCamera.gameObject.SetActive (false);
                                 number--;
                         } else {
                             mainPlayerCamera.gameObject.SetActive (true);
                             lastCamera = availableSpecCameras [number];
                             lastCamera.gameObject.SetActive (false);
                             availableSpecCameras = GameObject.FindGameObjectsWithTag("SpecCamera");
                             number = availableSpecCameras.Lenght;
                         }
                     }
             }
     
 
              Answer by DawdleDev · Mar 10, 2019 at 04:21 PM
@knightmar Here is some much simplified code for you. First, I used a List because it's much easier to work with. You can replace it with an array and it will still work. Second, I combined "availableSpecCameras" with "mainPlayerCamera" because you don't need them separate. If you ever need to refer to the player camera, just use "Cameras[0]" and you can always find it there. Third, I removed the references to the next and previous cameras because they were unnecessary. If you need to access them, use Cameras[counter - 1] for last camera and Cameras[counter + 1] for next (if counter == 0, last is Cameras.Count, if counter == Cameras.Count, next is 0).
 List<GameObject> Cameras = new List<GameObject> ();
     int counter = 0;
 
     void Start () {
         Cameras.Add (GameObject.FindGameObjectWithTag ("MainCamera"));
         Cameras.AddRange (GameObject.FindGameObjectsWithTag ("SpecCamera"));
     }
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.C)) {
             Cameras [counter].SetActive (false);
             if (counter < Cameras.Count) {
                 counter++;
             } else {
                 counter = 0;
             }
             Cameras [counter].SetActive (true);
         }
     }
 
               Hope this helped!
It's brilliant, it's much simpler and even helps with access in next parts of my script. Thank you very much!
Your answer