Why getting all the time IndexOutOfRangeException: Array index is out of range. exception when switching between cameras ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwitchCameras : MonoBehaviour {

    public Camera[] cameras;
    public int currentCamera = 0;
    public Vector3 lastCameraPositionOnSwitch;

    void Awake()
    {
        cameras = GameObject.FindObjectsOfType<Camera>();

        if (cameras.Length == 1)
        {
            Debug.LogError("Need more then 1 camera for switching..");
        }
        else
        {
            Debug.Log("Found " + cameras.Length + " cameras");
        }
    }

    void LateUpdate()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            if (currentCamera == cameras.Length)
                currentCamera = 0;

            cameras[currentCamera].enabled = false;
            cameras[(currentCamera = currentCamera + 1) % cameras.Length].enabled = true;
            lastCameraPositionOnSwitch = cameras[currentCamera].transform.position;
            
            Debug.Log(cameras[currentCamera].name + " Last position " + cameras[currentCamera].transform.position);
        }
    }
}

There are 3 cameras in this case there might be more or less in other cases.
I also try to keep the last switched to camera position with the variable astCameraPositionOnSwitch.

The exception is on the line:

lastCameraPositionOnSwitch = cameras[currentCamera].transform.position;

So i added a check and reset currentCamera to 0:

if (currentCamera == cameras.Length)
                    currentCamera = 0;

It’s not fixing it. Tried currentCamera == cameras.Length - 1
But then it will show only 2 cameras not the whole 3.

I think the problem is because you are incrementing the currentCamera value on line 33, but you check for it being too big on line 29.

That means that when you use it on line 34 the value is == the length of the array, which is 1 too many.

I would recommend that you change it to something like this:

         if (Input.GetKeyDown(KeyCode.C))
         {
             cameras[currentCamera].enabled = false;

             currentCamera++;

             if (currentCamera == cameras.Length)
                 currentCamera = 0;

             cameras[currentCamera % cameras.Length].enabled = true;
             lastCameraPositionOnSwitch = cameras[currentCamera].transform.position;
             
             Debug.Log(cameras[currentCamera].name + " Last position " + cameras[currentCamera].transform.position);
         }

You could shorten it a little by doing this too (notice the ++ in the front will increment it before testing against the array length):

         if (Input.GetKeyDown(KeyCode.C))
         {
             cameras[currentCamera].enabled = false;

             if (++currentCamera == cameras.Length)
                 currentCamera = 0;

             cameras[currentCamera % cameras.Length].enabled = true;
             lastCameraPositionOnSwitch = cameras[currentCamera].transform.position;
             
             Debug.Log(cameras[currentCamera].name + " Last position " + cameras[currentCamera].transform.position);
         }