title: How to add Persistent Listener to Button.onClick event in Unity Editor Script
body: <p>I am trying to do a simple thing:</p>
  <ol>
  <li>Create a new <code>GameObject</code></li>
  <li>Add a <code>Button</code> component to the <code>GameObject</code>.</li>
  <li>Add a persistent Listener to <code>Button</code>'s <code>OnClick</code> event.</li>
  </ol>
  <p>The method I am trying to register is in some other script. Here is piece of code that I am trying:</p>
  <pre><code>MyScript myScriptInstance = FindObjectOfType&lt;MyScript&gt;(); 
  var go = new GameObject();
  var btn = go.AddComponent&lt;Button&gt;();
      
  var targetinfo = UnityEvent.GetValidMethodInfo(myScriptInstance,
  &quot;OnButtonClick&quot;, new Type[]{typeof(GameObject)});
  
  var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),go, targetinfo, false);
  UnityEventTools.AddPersistentListener(btn.onClick, action);
  </code></pre>
  <p>MyScript.cs looks like this:</p>
  <pre><code>public class MyScript : MonoBehaviour
  {
      public void OnButtonClick(GameObject sender)
      {
          // do some stuff here.
      }
  }
  </code></pre>
  <p>When I run this code, Buttons Onclick listener is empty like this:</p>
  <p><img src="https://i.stack.imgur.com/tVfP5.png" alt="enter image description here" /></p>
  <p>If I change the line</p>
  <pre><code>var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),
  go, targetinfo, false);
  </code></pre>
  <p>to</p>
  <pre><code>var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),
  go, targetinfo, true);
  </code></pre>
  <p>I get :</p>
  <blockquote>
  <p>ArgumentException: method argument length mismatch
  System.Delegate.CreateDelegate (System.Type type, System.Object
  firstArgument, System.Reflection.MethodInfo method, Boolean
  throwOnBindFailure)</p>
  </blockquote>
  <p>I followed <a href="https://forum.unity3d.com/threads/how-to-create-persistent-listener-to-an-event.264228/#post-2173012" rel="nofollow noreferrer">these instructions</a> but don't know what went wrong here.</p>
  <p>Any kind of help is truly appreciated.</p>