title: Unity Button Delegates (AddListener)
body: <p>I'm trying to instantie several button using prefab.</p>
  
  <p>But I've got a problem to Delegate variable.</p>
  
  <pre><code>for(int i = 0; i &lt; _partnerList.partner.Count; i++)
  {
      GameObject _instance_partner_button = Instantiate(_prefab_partner_button);
      _instance_partner_button.name = "_button_"+_partnerList.partner[i].partner_name;
  
      _instance_partner_button.transform.SetParent(GameObject.Find("_UI_menu_partner").transform, false);
  
      // Change Value of The Button ----------------------------------------------------------------
      Text _instance_partner_button_text = _instance_partner_button.GetComponentInChildren&lt;Text&gt;();
      _instance_partner_button_text.text = _partnerList.partner[i].partner_name;
  
      Button _instance_partner_button_button = _instance_partner_button.GetComponent&lt;Button&gt;();
  
      int _tempInt = i;
  
      // Delegate to Another Script ---------------------------------------------------------------
      _instance_partner_button_button.onClick.AddListener(() =&gt; _scripts_myScripts_ui.AnimationMenuPartner(_tempInt));
      // -------------------------------------------------------------------------------------------
  }
  </code></pre>
  
  <p><em>The function AnimationMenuPartner() just Debug.Log(_tempInt);</em></p>
  
  <p>No matter which button I press the result is the same.</p>
  
  <p>I'm maybe missing something, as I see in this link it must be working correctly <a href="http://answers.unity3d.com/questions/908847/passing-a-temporary-variable-to-add-listener.html" rel="nofollow">Link</a></p>
  
  <p>Thanks you for your help.</p>
  
  <p>Edit : </p>
  
  <p>The Another Script with the function AnimationMenuPartner</p>
  
  <pre><code>public void AnimationMenuPartner(int _tempInt)
  {
      Debug.Log(_tempInt));
  }
  </code></pre>
  
  <p>The log output print : "4"</p>
  
  <p>Which is the length of the list.</p>