Creating a gameobject with many cameras components

Hi,
I’m brand new to unity

I am trying to write a script to create multiple cameras on one object.

public class CameraCreator : MonoBehaviour
{

    public  GameObject cam;
    // Start is called before the first frame update
    void Start()
    {
         for (int i=0; i<5; i++)
         {
        Camera cam = gameObject.AddComponent <Camera>() as Camera;
      
         }
 
          
      
    }
    // Update is called once per frame
    void Update()
    {
      
    }
}

I haven’t found out how to use lists in my case for components since my component is overwritten every time, can someone help me
or give me an example ?

You can’t add multiple camera components to the same game object and I’m not actually sure why you’d even want to.

5 cameras pointing on the same thing, why not :stuck_out_tongue:

I would like to place multiple cameras (with different configurations and positions) on a scene.

Add multiple gameobjects. Each with a camera component.

1 Like

Thank u for your answers. I’m brand new to Unity and struggle with the basics a bit.

I tried to create multiple objects this way :

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

public class CameraCreator : MonoBehaviour
{
 
    // Start is called before the first frame update
  
    public GameObject prefab;
  
    void Start()
    {
            GameObject prefab = Resources.Load("Assets/Ressources/Main Camera.prefab") as GameObject;

        for (int i=0; i<5;i++)
        {
             Instantiate(prefab, new Vector3(3*i,0,0), Quaternion.identity);

        }
  
  
          
      
    }
    // Update is called once per frame
    void Update()
    {
      
    }
}

and I get the following error :

Problem solved, thank u to all :slight_smile:

1 Like