I need to instantiate an object at runtime, where the number of objects are based on a txt file (number of lines). All objects need to be clickable (onClick() event) and when they are pressed a tooltip must appear. Tooltips are specific ( Object1 -> Tooltip1, Object2 -> Tooltip2). Tooltips are just a Panel and they consist in some other Panels and Buttons. One of these create a new Panel. Also these Panel are specific (Tooltip1 -> Panel1 and so on). I create a prefab for each of three objects.

So, Object1 - onClick() -> Tooltip1 - onClick() -> Panel1. At runtime.

How can I keep reference of an object create at runtime?

**My first solution**:

1. Create a Empty object and assign a script to it with a public variable (Object prefab).
2. Instantiate Object prefab:

```
for (int i = 0; i < numberOfObject; i++)
{
   var instance = Instantiate(m_ObjectPrefab);
   instance.name = "Object_" + m_nameObject[i];

```
3. Instantiate one Tooltip for each Object:

```
    var instancePanelTooltip = Instantiate(m_panelTooltipPrefab, m_canvas.transform);
    instancePanelTooltip.name = "Panel_Tooltip_" + m_nameObject[i];

```
4. Instantiate one Panel for each Tooltip:

```
   var instancePanel = Instantiate(m_panelPrefab, m_canvas.transform);
   instancePanel.name = "Panel_" + m_nameObject[i];
   instancePanel.SetActive(false);
   instancePanelTooltip.SetActive(false);

```
5. Add event handler for Object

```
    DetectClickOnObject scriptDetectClickPanelTooltip = instance.AddComponent<DetectClickOnObject>();
    scriptDetectClickPanelTooltip.SetTooltip(instancePanelTooltip);

```
6. Add event handler for button on Tooltip

```
    DetectClickOnObject scriptDetectClickPanel = buttonOpenPanel.AddComponent<DetectClickOnObject>();
    scriptDetectClickPanel.SetPanel(instancePanel);
}

```

**Issue with this solution:**

7. I will instantiate 3 object (Object, Tooltip, Panel) for each line of file. For Object it is okay, but it is not with Tooltip and Panel, because only one Tooltip is active among all (same for Panel).
8. I just avoid the problem of reference because I create in the same place all objects (one for each element), but what can I do if I need to get access to Tooltip2 or Panel3 without a reference (I am trying to avoid Find and similar).

**Conclusion of the first solution:** Solution is working, but I think there is a better way of doing that (avoid create so much object and keep reference in the right way).

**My second solution (guidelines):** 

1. I am trying to create a class to keep reference of all object create at runtime.
2. I want to create an instance of Object for each lines, but I want just one Tooltip and Panel for all Object and changing properties according to Object clicked. So Object is create at runtime, but Tooltip and Panel are already in the scene but not active.
3. I need a Register Event Manager to add onClick() event at runtime on Objects and it need to handle properties to be set on Tooltip and Panel based on clicked Object.

**Problems with second solution:** Referring to 1) I tried to follow [that](https://gamedev.stackexchange.com/questions/135209/how-to-store-references-to-scene-objects-in-prefabs), but I ended up with nothing. I am lost between singleton, static and something else. Referring to 2) I think it can be easy, I just need to cut away some of the first solution. Referring to 3) I can not do more if I do not have a class reference manager.

**What I am looking for:**

1. Is first solution so bad? If I look at code I am disgusted by it, it is far away from elegance (or something similar).
2. Can you suggest me how can I keep track of reference create at runtime with a Reference Manager? And how use it?

**@Behnam Sattar suggestion:**
As DataModell class,

```
public class DataModelPOI
{
    public string m_namePOI { get; private set; }
    public string m_locationPOI { get; private set; }
    public Vector2d m_positionPOI { get; private set; }

    public GameObject m_gameObject_POI;
    public GameObject m_gameObjectTooltip;
    public GameObject m_gameObjectPanel;

    public DataModelPOI(string namePOI, string locationPOI, Vector2d positionPOI)
    {
        this.m_namePOI = namePOI;
        this.m_locationPOI = locationPOI;
        this.m_positionPOI = positionPOI;
    }
}

```

As DataManager,

```
public class POIManager : MonoBehaviour
{
    List<DataModelPOI> dataCollectionPOI = new List<DataModelPOI>();

    void Start()
    {
        ReadFile();

        SpawnPOI();
    }

    void Update()
    {
        int count = dataCollectionPOI.Count;
        for (int i = 0; i < count; i++)
        {
            UpdatePOIPosition();
        }
    }

    void ReadFile()
    {
        TakeDataFromFile();
        for (int i = 0; i < ength; i++)
        {
            DataModelPOI dataPOI = new DataModelPOI(m_namePoi[i], m_namePoi[i], _locations[i]);
            dataCollectionPOI.Add(dataPOI);
        }
    }

    private void SpawnPOI()
    {
        for (int i = 0; i < dataCollectionPOI.Count; i++)
        {
            DataModelPOI dataPOI = dataCollectionPOI[i];
            var instance = Instantiate(m_POIPrefab);
            instance.name = "POI_" + m_namePoi[i];
            dataPOI.m_gameObject_POI = instance;
            dataPOI.m_gameObjectTooltip = m_panelTooltipPOI;
            dataPOI.m_gameObjectPanel = m_panelPOI;
        }
    }

```

Now I need to register Event associate to GameObject instantiate before. I want to do that in my EventManager. How can I point to `dataCollectionPOI` in EventManager class created and feeded in DataManager? Thanks for your time.