- Home /
[BUG maybe] [picture acctached] Event Trigger component does not display the Event Type added from script
Hey!
I was looking how to hover over the UI button in Unity 5 (or Unity 2019). Found the perfect answer here:
https://answers.unity.com/questions/854251/how-do-you-add-an-ui-eventtrigger-by-script.html
by @Phles
It works: it adds the event trigger via script, adds the event type via script, chooses pointer enter via script and even the function to execute - all that can be done manually by dragging in the inspector.
However, though functionally everything is working, the inspector shows a pure component - an empty list - i.e. without the Event Type or the Function chosen via the script. I assume this is a bug, if no, could you please tell me how to make the inspector display this. Thank you!
The image: The script is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //UI
using UnityEngine.EventSystems; //hover
public class abc_abc : MonoBehaviour {
// Start is called before the first frame update
void Start() {
this.transform.parent.gameObject.AddComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener((eventData) => { do_debug_log2(); });
this.transform.parent.gameObject.GetComponent<EventTrigger>().triggers.Add(entry);
}
public void do_debug_log2() {
Debug.Log("mouse is over the object");
}
}
Answer by Bunny83 · Aug 14, 2019 at 09:28 PM
There is no bug. A UnityEvent (which is used by the EventTrigger) has two seperate internal lists:
The serialized "persistent calls" list
The dynamic callback list.
The inspector will only show serialized data which is the persistent list. How would you even display a event delegate that represents a closure. Specifically this one:
(eventData) => { do_debug_log2(); }
The created closure object can not be serialized by Unity. Anyways dynamically added listeners (AddListener / RemoveListener) will only work on the dynamic list. The persistent list is managed by the Unity editor and the property drawer for the UnityEvent type. The Unity editor has tools to add / remove methods to persistent calls. However this can only be done through editor code, not at runtime. Also persistent calls have several limitations. For example the method must be part of a UnityEngine.Object derived type since the reference to the object need to be serialized.
To write editor tools which can add / remove / modify persistent listeners, Unity provides the UnityEventTools class. As I said this is an editor class and can not be used at runtime.
So dynamically added listeners will still be called just fine, but there's no way to display them in the inspector unless you somehow visualize them yourself with an editor script.
Note that the "event type" is displayed properly. It's the header of that section: "Pointer Enter". Though this event type does not have any persistent listeners and therefore the list is empty.
0) Pardon me, you are right, the "event type" is Indeed, displayed properly. I apologize.
1) I remeber your username, you helped me sometime ago, thank you again!
2) Thank you for a careful expalantion and a quick reply.
3) I am sorry, my knowledge of program$$anonymous$$g is not enough to comprehend your answer, I think I understood the basics, but the details are not clear: "Though this event type does not have any persistent listeners and therefore the list is empty." So am I correct, that: There is a way to make a listener "persistent" via script and in that case it will be shown in the inspector?
If you could clarify, please do. Thank you anyway, you already helped!
Sorry for the late reply but I was on vacation. Did you actually read my answer carefully? I said:
The persistent list is managed by the Unity editor and the property drawer for the UnityEvent type. The Unity editor has tools to add / remove methods to persistent calls. However this can only be done through editor code, not at runtime. Also persistent calls have several limitations. For example the method must be part of a UnityEngine.Object derived type since the reference to the object need to be serialized.
Your answer
![](https://koobas.hobune.stream/wayback/20220612210641im_/https://answers.unity.com/themes/thub/images/avi.jpg)