title: Instantiating a list of gameobjects in Unity C#
body: <p>How can I instantiate a list of GameObject in Unity3D using c#?
  I fill the list with prefabs manually in inspector window.</p>
  
  <p><a href="https://i.stack.imgur.com/7FCaE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7FCaE.png" alt="enter image description here"></a></p>
  
  <p>Below is the code I've written in Deck.cs, but I get "Object reference is not set to an instance of an object". If you have a solution with an array, that will be appreciated as well.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using UnityEngine;
  using UnityEngine.UI;
  
  namespace Assets
  {
      class Deck:MonoBehaviour
      {
          public List&lt;GameObject&gt; deck;
          public void Fill()
          {
              GameObject c1 = Instantiate(deck[0]);
              GameObject c2 = Instantiate(deck[1]);
              GameObject c3 = Instantiate(deck[2]);
              GameObject c4 = Instantiate(deck[3]);
              GameObject c5 = Instantiate(deck[4]);
              GameObject c6 = Instantiate(deck[5]);
          }
  
      }
  }
  </code></pre>
  
  <p>I also tried doing it with an array and I get 'The object you want to instantiate is null'</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using UnityEngine;
  using UnityEngine.UI;
  
  namespace Assets
  {
      class Deck:MonoBehaviour
      {
          public GameObject card1;
          public GameObject card2;
          public GameObject card3;
          public GameObject card4;
          public GameObject card5;
          public GameObject card6;
  
          public GameObject[] deck;
  
          public void Start ()
          {
              deck = new GameObject[5];
              GameObject c1 = Instantiate(card1) as GameObject;
              GameObject c2 = Instantiate(card2) as GameObject;
              GameObject c3 = Instantiate(card3) as GameObject;
              GameObject c4 = Instantiate(card4) as GameObject;
              GameObject c5 = Instantiate(card5) as GameObject;
              GameObject c6 = Instantiate(card6) as GameObject;
          }
  
      }
  }
  </code></pre>