Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Adding an event trigger in C# is not working

Discussion in 'UGUI & TextMesh Pro' started by Athomield3D, Jun 21, 2016.

  1. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    Hi I'm trying to bind an event trigger at run time in c#

    Code (CSharp):
    1. EventTrigger off_trigger = Pad.GetComponent<EventTrigger>( );
    2.         EventTrigger.Entry off_entry = new EventTrigger.Entry( );
    3.         off_entry.eventID = EventTriggerType.PointerUp;
    4.         off_entry.callback.AddListener( ( data ) => { someFunc() } );
    5.         off_trigger.triggers.Add(off_entry);
    But it's giving me this Empty BaseEventData, Isn't supposed to create a base event data and bind it to a function ?

     
  2. TrickyHandz

    TrickyHandz

    Joined:
    Jul 23, 2010
    Posts:
    196
    Only functions assigned through the inspector will show up in a UnityEvent property drawer. If you assign a listener through code it will not populate the inspector. A benefit to using a UnityEvent is the ability to assign a delegate via the inspector rather than through code. If you are only trying to create pointer events in your class, I would recommend implementing the IPointerDownHandler, IPointerUpHandler, and IPointerClickHandler interfaces.
     
    Athomield3D likes this.
  3. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    Thanks, working.
     
  4. ChristopherWood

    ChristopherWood

    Joined:
    Mar 24, 2016
    Posts:
    3
    Would you share your solution? Please!