Search Unity

Creating a gameobject with many cameras components

Discussion in 'General Discussion' started by Faiz_Belk, Mar 29, 2021.

  1. Faiz_Belk

    Faiz_Belk

    Joined:
    Mar 1, 2021
    Posts:
    41
    Hi,
    I'm brand new to unity

    I am trying to write a script to create multiple cameras on one object.
    Code (CSharp):
    1. public class CameraCreator : MonoBehaviour
    2. {
    3.  
    4.     public  GameObject cam;
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.          for (int i=0; i<5; i++)
    9.          {
    10.         Camera cam = gameObject.AddComponent <Camera>() as Camera;
    11.      
    12.          }
    13.  
    14.          
    15.      
    16.     }
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.      
    21.     }
    22. }
    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 ?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    You can't add multiple camera components to the same game object and I'm not actually sure why you'd even want to.
     
  3. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    5 cameras pointing on the same thing, why not :p
     
  4. Faiz_Belk

    Faiz_Belk

    Joined:
    Mar 1, 2021
    Posts:
    41
    I would like to place multiple cameras (with different configurations and positions) on a scene.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    Add multiple gameobjects. Each with a camera component.
     
    Antypodish likes this.
  6. Faiz_Belk

    Faiz_Belk

    Joined:
    Mar 1, 2021
    Posts:
    41
    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 :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraCreator : MonoBehaviour
    6. {
    7.  
    8.     // Start is called before the first frame update
    9.  
    10.     public GameObject prefab;
    11.  
    12.     void Start()
    13.     {
    14.             GameObject prefab = Resources.Load("Assets/Ressources/Main Camera.prefab") as GameObject;
    15.  
    16.         for (int i=0; i<5;i++)
    17.         {
    18.              Instantiate(prefab, new Vector3(3*i,0,0), Quaternion.identity);
    19.  
    20.         }
    21.  
    22.  
    23.          
    24.      
    25.     }
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.      
    30.     }
    31. }
    32.  
    and I get the following error :

     
    Last edited: Mar 29, 2021
  7. Faiz_Belk

    Faiz_Belk

    Joined:
    Mar 1, 2021
    Posts:
    41
    Problem solved, thank u to all :)
     
    stain2319 likes this.