- Home /
The question is answered, right answer was accepted
Issues implementing UI event triggers via script
I have been trying to add Event triggers to my UI elements via script and get them to work. But the events just never fire or do anything, and I can't find any answers as to what could be causing the issue. Here is the code used to add the event:
EventTrigger trigger = abilityIcon.AddComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener((eventData) =>
{
Debug.Log("Event!");
});
trigger.triggers.Add(entry);
And here is an image of the UI element in my editor after running the game:
I've tried deleting every other UI element except for the one with the event trigger so that I know nothing is blocking it. But the function never seems to fire under any event type.
If anybody has any ideas about what could be causing the issues I'd really appreciate it!
Confirm your Canvas has a GraphicRaycaster component, and if so, double check it's settings. If not, that's probably your issue; just add one.
Thanks, it was a combination of this and the icon being blocked by another UI object in the canvas. It's working now.
Answer by jeango · Nov 01, 2017 at 05:23 PM
in my experience, listeners that are added via code are not visible in the inspector when the game is running.
I have read that they will not appear in the inspector, but the issue is not simply that they aren't appearing in the inspector, they aren't firing either.
$$anonymous$$oving the cursor over the button does not print a debug log message. I've never been able to get any of the event triggers I've added via script to do anything at all.
what I meant to say is that there’s nothing wrong with your code, so the issue is not about the registration of the listener but rather that the event just never happens. I think what Glurth is saying makes sense. You should investigate the reasons why the event doesn’t occur