how do I deactivate all cameras in a list except for the one that is currently in use?

Viewed 43

I have a list of cameras that the player can switch through using the up and down keys to look around with.

Instead of all cameras being on at the same time when the game starts, I would like it if they were all off until the player switches over to them, then it becomes active. then if I switch again the next camera becomes active and the previous becomes inactive and so on and so forth.

How do I do this?

public class CameraManager : MonoBehaviour
{
    [SerializeField]
    public List<Camera> cameras = new List<Camera>();
    
    public int currentCamera;

    public void Awake ()
    {
        cameras.Add(Camera.main);
        cameras.AddRange(FindObjectsOfType<Camera>());
    }

    void incCamera()
    {
        cameras [currentCamera].enabled = false;

        currentCamera++;

        if(currentCamera >= cameras.Count){
            currentCamera = 0;
        }

        cameras [currentCamera].enabled = true;
    }

    void decCamera()
    {
        cameras [currentCamera].enabled = false;
        currentCamera--;

        if(currentCamera < 0){

            currentCamera = cameras.Count-1;
        }
        cameras [currentCamera].enabled = true;
    }

    public void Update()
    {
        if (Input.GetKeyUp(KeyCode.UpArrow))
        {
            incCamera();
        }
        if (Input.GetKeyUp(KeyCode.DownArrow))
        {
            decCamera();
        }
0 Answers
Related