Example of setting up a button OnClick event via scripting?

FYI - this is cross posted from the 4.6 forum. I wasn’t getting any hits there, so I figured I’d try here as well.

My question is this:

Can anyone point me to an example of setting up a button OnClick event via scripting in 4.6?

The new GUI tutorials do a good job of showing how to use the inspector to make a button call arbitrary functions on an OnClick event (e.g. here). I figure there must be a way to do the same thing via scripting - to give a button a list of functions (and arguments for those functions) to call when it is clicked. However, I’m getting lost in the documentation (e.g. Event Tools, ButtonClickedEvent). There are lots of functions, but only very brief descriptions, and the meaning is beyond me.

Any advice would be greatly appreciated.

for (int i =1; i<=50; i++) {

  button.name=PlayerPrefs.GetString ("LeveltextNumber" + i.ToString ());

  button.button.onClick.AddListener(delegate { test(button.name); });


public void test(string ok){

	Debug.Log (ok);


it should solve the problem

please accept the answer if this solve your problem

Just like this: xxxBtn.onClick.AddListener(YourDisposeFunction);