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>());


    }
}

,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>());


    }
}

Hi!

List.Add() expects one item to add to the list, and the compiler doesn’t know you you want to covert many cameras into one.

You probably want to be using the List.AddRange() function, which will accept many items, instead of just one.

P.S. You are adding all cameras to your list every frame, you probably want to check if the camera is in the list already with List.Contains(). You can also consider using a HashSet or Dictionary as these collections don’t allow duplicates.