How do you create a list of Cameras?

Hello,

I’m trying to make a script that contains a list of Cameras. The intention is for the player to have a number of Cameras that they can switch back and forth through at will (kind of like Friday night at Freddy’s) and later on the player can add more cameras which is why I decided on using a list instead of an Array.

However, when I tried to make a script with that list, I got this error

Argument 1: cannot convert from ‘UnityEngine.Camera’ to ‘UnityEngine.Camera’ [Assembly-CSharp]

This is the code I wrote:

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

public class CameraList : MonoBehaviour
{

        public List <Camera> _cameras = new List <Camera>();

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        _cameras.Add(Camera.main);

        _cameras.Add(FindObjectsOfType<Camera>());


    }
}

The error is occurring because you are getting an array of objects and adding them into the lost in a single line…


To fix it, change the line of code to :

void Update() 
{
    _cameras.Add(Camera.main); 

    Camera[] Cams = FindObjectsOfType<Camera>();
    foreach (Camera Cam in Cams)
    {
        _cameras.Add(Cam);
    }
}