Setting Button.onClick with a script in the editor

Hey, although I’ve seen similar questions asked and answered, I’m still completely stuck. I am trying to use UnityEventTools.AddPersistentListener to add a listener to a button in an editor script, but I get an uninformative error stating that The class null does not derive from UnityEngine.Object

I have a function that takes an integer input, and want to add it as an onClick listener as you would do in the editor

Here’s my current buggy attempt (C#) . g is an int, and OpenMenuOf is a method of EnemyListControlScript

`EnemyListControlScript E = GameObject.Find(“EnemyListController”).GetComponent();
Button btn = myButton.transform.GetChild(0).GetComponent();

UnityEventTools.AddPersistentListener(btn.onClick, new UnityAction(delegate { E.OpenMenuOf(g); }));`

Not sure why this doesn’t work :frowning: any help would be much appreciated, thanks!

After far too many attempts, I got it working, will post code here in case I can help anyone in a similar situation :confused:

int g = yourInteger;  
//E = yourInstanceOfTargetComponent  
Button btn = yourButtonComponent;  

System.Type[] arguments = new System.Type[1];  
arguments[0] = typeof(int);  
System.Reflection.MethodInfo myInfo = UnityEvent.GetValidMethodInfo(E, "OpenMenuOf", arguments);  
UnityAction ua = System.Delegate.CreateDelegate(typeof(UnityAction), E, myInfo) as UnityAction;  

UnityEditor.Events.UnityEventTools.AddIntPersistentListener(btn.onClick, ua, g);