Search Unity

How to give Parameters to Button Events via script?

Discussion in 'UGUI & TextMesh Pro' started by UndeadLeech, Aug 22, 2014.

  1. UndeadLeech

    UndeadLeech

    Joined:
    Jan 26, 2014
    Posts:
    3
    I try to get a Button prefab working. My only problem is, that I can't call the function I wan't to call. I want to call a function which needs the text of the button as parameter. So now I wanted to use OnClick.AddListener(method) but like that I'm unable to send parameters I can only call a function without any parameters. I'm using c# btw.
    Any suggestions?
     
  2. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    My Button binder script wraps around the button's AddListener method. When the button is pressed, the binder handles the event and calls the target method. If I have a parameter, I pass that along for the ride.
     
    Last edited: Aug 22, 2014
    Tim-C likes this.
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    like nventimiglia says you can wrap this when you make the call.

    Something like:

    Code (csharp):
    1.  
    2. myButton.AddListener (delegate { myFunction (myButton.text);});
    3.