I am creating a version of the Tower of Hanoi puzzle. The way it is represented in the Unity hierarchy is
-Game Objects
-----PegA
---------Arm
---------Base
Sorry I didn't know how to represent the hierarchy.
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.
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.
This is part of my ring class
public class ring : MonoBehaviour
{
public bool locked, resting;
private float startX, startY, deltaX, deltaY;
private Vector3 mousePos, beforeDrag;
private List<GameObject> 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"));
}
}
The error I'm getting is "Object reference not set to an instance of an object"
Can someone possibly explain?