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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. 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.