Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Button action with arguments

Discussion in 'UGUI & TextMesh Pro' started by Diablo404, Aug 21, 2014.

  1. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Hey guys,

    From this forum I found this:

    Code (JavaScript):
    1. var iconT = (Transform)Instantiate(iconPrefab);
    2. var button = iconT.GetComponentInChildren<Button>();
    3. button.onClick.AddListener(FunctionToCall);

    But is it possible to pass some functions with parameters?
    So we could handle wether it's button 0,1,2 etc ... ?

    Thanks in advance
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    In this case you know the button you are hooking up to so you can use inline delegates:

    button.onClick.AddListener( delegate {FunctionToCall(myargument);} );
     
    BMayne likes this.
  3. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Well, to make it working, in UnityScript, I had to change for:
    button.onClick.AddListener( function() {FunctionToCall(myargument);} );

    But yep, thanks for the tip, it's working now!
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    Ah sorry, I just live in c# so much these days and wasn't thinking :)
     
  5. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    and for add PersistentListenerState ?