I want to create an array of objects at runtime : <p>I am creating a version of the Tower of Hanoi puzzle. The way it is represented in the Unity hierarchy is</p>

<p>-Game Objects<br>
-----PegA<br>
---------Arm<br>
---------Base<br></p>

<p>Sorry I didn't know how to represent the hierarchy.</p>

<p>There are 3 "pegs" and have 7 "rings" as objects in the scene. The pegs and rings are on the same level in the hierarchy.</p>

<p>It is obvious that I can "SerializeField" the ring class and just click and drag each Peg onto them in the inspector but what I want to do is just add them at runtime in the code. This is what I tried.</p>

<p>This is part of my ring class</p>

<pre><code>public class ring : MonoBehaviour
{
    public bool locked, resting;
    private float startX, startY, deltaX, deltaY;
    private Vector3 mousePos, beforeDrag;

    private List&lt;GameObject&gt; pegs;

    // Start is called before the first frame update
    void Start()
    {
        startX = transform.position.x;
        startY = transform.position.y;
        locked = false;
        pegs.Add(GameObject.Find("PegA"));
        pegs.Add(GameObject.Find("PegB"));
        pegs.Add(GameObject.Find("PegC"));
    }
}
</code></pre>

<p>The error I'm getting is "Object reference not set to an instance of an object"</p>

<p>Can someone possibly explain?</p>
