title: Unity onClick.addlistener not working
body: <p>I load a Canvas prefab at runtime when an event occurs. The canvas simply has a Panel inside it, which in turn has 2 buttons. I'm trying to add OnClick events to both these buttons in the script, but it only works for the first button somehow!
  I have the following two lines of code one after the other:</p>
  
  <pre><code>GameObject.Find("RestartButton").GetComponent&lt;Button&gt;().onClick.AddListener(() =&gt; RestartClicked());
  GameObject.Find("ViewButton").GetComponent&lt;Button&gt;().onClick.AddListener(() =&gt; ViewClicked());
  </code></pre>
  
  <p>The callback only works for the RestartButton, and not for the ViewButton.
  It may well be a very small thing, but I searched Google and Bing extensively and remain clueless, so any help would be appreciated.</p>
  
  <p>Thanks! </p>
  
  <h2>Edit:</h2>
  
  <p>The script instantiates the prefab and tries to reference the two buttons in the prefab through GameObject.Find(), given that once Instantiate is called the Canvas should be active in the heirarchy.
  I also opened up the part of the code that attaches buttons to the listener for debugging. I dont think it attaches the listener at all.</p>
  
  <pre><code>void Start () {
  
      SetupScene();
  
      var prefab = Resources.Load("RestartOrViewPrefab");
      if (prefab == null)
      {
          Debug.LogAssertion("Prefab missing!");
          return;
      }
      restartCanvas = (GameObject)Instantiate(prefab);
      Button btn = GameObject.Find("ViewButton").GetComponent&lt;Button&gt;();
      btn.onClick.RemoveAllListeners();
      btn.onClick.AddListener(() =&gt; ViewToggle());
      Button btn2 = GameObject.Find("RestartButton").GetComponent&lt;Button&gt;();
      btn2.onClick.AddListener(() =&gt; RestartClicked());
  }
  
  private void RestartClicked()
  {
      Debug.Log("RESTARTING");
      SetupScene();
  }
  
  private void ViewToggle()
  {
      Debug.Log("TOGGLE");
      if (freshStart)
      {
          //Something here
          freshStart = false;
      }
      else
      {
          //Something else here
          freshStart = true;
      }
  }
  </code></pre>