- Home /
EventTrigger runtime added callbacks to OnPointerClick/OnPointerUp/OnPointerDown don't work.
I am trying to get a UI component to react to clicking by using an EventTrigger component.
I have made myself a small extension method for this purpose which reads as follows:
public static void AddEventTrigger (
this EventTrigger eventTrigger,
EventTriggerType eventTriggerType,
UnityAction<BaseEventData> callback)
{
EventTrigger.Entry entry = new EventTrigger.Entry { eventID = eventTriggerType };
entry.callback.AddListener (callback);
eventTrigger.triggers.Add (entry);
}
This seems to be the consensus on how you should add events to an EventTrigger at runtime, and it does work for OnPointerEnter and OnPointerExit.
It does not, however, work for anything involving clicks, whether it is OnPointerClick or OnPointerUp or OnPointerDown. These 3 events in particular also don't seem to work even if they are populated manually through the inspector.
The object in question has an Image component that is a raycast target so that should not be the issue, especially since other ones seem to work.
Has anyone here ran into this issue before and/or is there something I'm missing?
Answer by GRobecchi · Nov 06, 2019 at 01:02 PM
Answering my own question, as we managed to solve this through some combing of our objects.
Apparently, EventTrigger events don't all behave the same way. Some are applied to all objects regardless of order, while others are stopped at the first invocation.
The object in question had a child object also with its own EventTrigger (a vestige of a prior implementation) that was apparently consuming the click events. Removing the component fixed the issue.