title: How to maintain reference to object instantiate at runtime in Unity
body: <p>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.</p>
  
  <p>So, Object1 - onClick() -> Tooltip1 - onClick() -> Panel1. At runtime.</p>
  
  <p>How can I keep reference of an object create at runtime?</p>
  
  <p><strong>My first solution</strong>:</p>
  
  <ol>
  <li>Create a Empty object and assign a script to it with a public variable (Object prefab).</li>
  <li><p>Instantiate Object prefab:</p>
  
  <pre><code>for (int i = 0; i &lt; numberOfObject; i++)
  {
     var instance = Instantiate(m_ObjectPrefab);
     instance.name = "Object_" + m_nameObject[i];
  </code></pre></li>
  <li><p>Instantiate one Tooltip for each Object:</p>
  
  <pre><code>    var instancePanelTooltip = Instantiate(m_panelTooltipPrefab, m_canvas.transform);
      instancePanelTooltip.name = "Panel_Tooltip_" + m_nameObject[i];
  </code></pre></li>
  <li><p>Instantiate one Panel for each Tooltip:</p>
  
  <pre><code>   var instancePanel = Instantiate(m_panelPrefab, m_canvas.transform);
     instancePanel.name = "Panel_" + m_nameObject[i];
     instancePanel.SetActive(false);
     instancePanelTooltip.SetActive(false);
  </code></pre></li>
  <li><p>Add event handler for Object</p>
  
  <pre><code>    DetectClickOnObject scriptDetectClickPanelTooltip = instance.AddComponent&lt;DetectClickOnObject&gt;();
      scriptDetectClickPanelTooltip.SetTooltip(instancePanelTooltip);
  </code></pre></li>
  <li><p>Add event handler for button on Tooltip</p>
  
  <pre><code>    DetectClickOnObject scriptDetectClickPanel = buttonOpenPanel.AddComponent&lt;DetectClickOnObject&gt;();
      scriptDetectClickPanel.SetPanel(instancePanel);
  }
  </code></pre></li>
  </ol>
  
  <p><strong>Issue with this solution:</strong></p>
  
  <ol start="7">
  <li><p>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).</p></li>
  <li><p>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).</p></li>
  </ol>
  
  <p><strong>Conclusion of the first solution:</strong> 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).</p>
  
  <p><strong>My second solution (guidelines):</strong> </p>
  
  <ol>
  <li>I am trying to create a class to keep reference of all object create at runtime.</li>
  <li>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.</li>
  <li>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.</li>
  </ol>
  
  <p><strong>Problems with second solution:</strong> Referring to 1) I tried to follow <a href="https://gamedev.stackexchange.com/questions/135209/how-to-store-references-to-scene-objects-in-prefabs">that</a>, 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.</p>
  
  <p><strong>What I am looking for:</strong></p>
  
  <ol>
  <li>Is first solution so bad? If I look at code I am disgusted by it, it is far away from elegance (or something similar).</li>
  <li>Can you suggest me how can I keep track of reference create at runtime with a Reference Manager? And how use it?</li>
  </ol>
  
  <p><strong>@Behnam Sattar suggestion:</strong>
  As DataModell class,</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>As DataManager,</p>
  
  <pre><code>public class POIManager : MonoBehaviour
  {
      List&lt;DataModelPOI&gt; dataCollectionPOI = new List&lt;DataModelPOI&gt;();
  
      void Start()
      {
          ReadFile();
  
          SpawnPOI();
      }
  
      void Update()
      {
          int count = dataCollectionPOI.Count;
          for (int i = 0; i &lt; count; i++)
          {
              UpdatePOIPosition();
          }
      }
  
      void ReadFile()
      {
          TakeDataFromFile();
          for (int i = 0; i &lt; 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 &lt; 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;
          }
      }
  </code></pre>
  
  <p>Now I need to register Event associate to GameObject instantiate before. I want to do that in my EventManager. How can I point to <code>dataCollectionPOI</code> in EventManager class created and feeded in DataManager? Thanks for your time.</p>