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

Assigning functions to event triggers at runtime (javascript)

Discussion in 'Scripting' started by apiotuch, Oct 29, 2015.

  1. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    Does anyone know how to do this in javascript? I am using Unity 4.6 and no I can't upgrade to 5.

    Basically this is what I have, and I don't get errors, but my function doesn't fire. The entry does get added at runtime since I see the Select event in the inspector. However, the function doesn't appear in the inspector. I tried this using c# and works, but that doesn't appear in the inspector either. I don't care if it appears or not in the inspector, I just want the function to fire.

    Code (JavaScript):
    1.  
    2. function Start ()
    3. {
    4.      var trigger : EventTrigger = gameObject.GetComponent (EventTrigger);
    5.      var entry : EventTrigger.Entry = EventTrigger.Entry();
    6.      entry.eventID = EventTriggerType.Select;
    7.      entry.callback = EventTrigger.TriggerEvent ();
    8.      var l_callback : UnityEngine.Events.UnityAction.<UnityEventBase> = UnityEngine.Events.UnityAction.<UnityEventBase>(OnSelectOption);
    9.      entry.callback.AddListener(function (){l_callback;});
    10.      trigger.delegates.Add (entry);
    11. }
    12.  
    13. public function OnSelectOption (baseEvent:UnityEventBase)
    14. {
    15.      Debug.Log ("Hello World");
    16. }
    I believe "entry.callback.AddListener(function (){l_callback;});" is not correct and l_callback is not assigned as a listener. So the syntax for this line is what I am having an issue with. I usually code in c#, but due to the project I am working on being all in javascript, it has to be in javascript (e.g) first pass methodology won't work since I need cross communication between scripts and assets.
     
  2. apiotuch

    apiotuch

    Joined:
    Oct 8, 2009
    Posts:
    42
    I figured it out I'll post a solution later today.
     
  3. Noxury

    Noxury

    Joined:
    Mar 8, 2015
    Posts:
    22
    We wait...The day.