Unable to assign callbacks in "Event Trigger" component in the inspector
I have been trying to assign delegate functions in scripts that are attached to game objects. I thought that there must be a better way to handle mouse clicks than the standard polling that everybody does in update()
. I found the Unity tutorial about the click adventure game.
I have been trying high and low to replicate what they achieve, but nothing is working. I tried several Unity versions, thinking maybe it's a bug - currently using 2019.2.5f1, so I tried 2019.2.6f1, and 2019.3.0b3 and I get same results.
When I try to add any function that takes a BaseEventData
or PointerEventData
argument, they never show up in the list. Other functions that take a String
argument or have no arguments show up fine. I scoured the net looking for tutorials, and most deal with customized even systems, or just deal with 0-argument functions.
I can add the functions as listeners through code, as they work when tested, but never through the interface which makes code not modular and beats the purpose of using the interface for assignment.
Has the API changed somehow? Or am I missing something? Anybody else experiencing such behavior?
Here is a screencap of what the interface lists. Notice that the PointerClick
event does not have the BaseEventData
parameter.
Thank you Helium. That video is from 2014. Didn't know it still applies.
The Unity video tutorial I referred to is from 2016. Here he's writing the delegate function: https://youtu.be/zsOcALVNl0s?t=2083
And here they're registering the function in the "Event Trigger" component: https://youtu.be/zsOcALVNl0s?t=2579
It's like they're using a different version of the "Event Trigger" component!
The Unity scripting API manual also shows an example of how to register as a delegate with the EventTrigger
object (second script example): https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.EventTrigger.html
I changed the parameter types to the ones you mentioned, and the function shows in the list. If the arguments passed should be primitive types, how are we going to get the event data that triggered the call-back ?
So far, it works by registering through code, though. So I think I think I'd follow $$anonymous$$urphy: "If it works, don't fix it".
Thanks again.
$$anonymous$$y bad, I was wrong, I've done a quick test with a very simple class
using UnityEngine;
using UnityEngine.EventSystems;
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
public void Test(BaseEventData baseEventData)
{
}
}
The Test
function appears in the Dynamic BaseEventData
list in the OnPointerClick
event of an event trigger.
The very strange thing in the screenshot you have sent is that your OnPointerClick
event does not have any parameter in the inspector:
I am on Unity 2019.1.13, so I don't believe this thing has changed on Unity 2019.2+
have you tried to remove the component and attach it again?
I have noticed that as well, which made me remark that it looks like they were using another version of the component than I was.
Now that I see that your version looks different than $$anonymous$$e as well, it could be a setting in my version of Unity maybe ? Or could it be a different version of the component/system ?
I removed the component and attached it back. Started 2 new clean projects. Tried the component on normal objects and on prefabs as well. All the attempts have the same effect.
I just removed Unity and Hub completely. Reinstalled. Using Unity 2019.2.5f1. Same effect. I checked the dates on the EventTrigger.cs and its Editor script. They were dated Sept 4 this year. So they are recent.
Edit to add: It looks like a reflection problem. So I checked the .Net version setting and used .Net 4.x, and IL2CPP ins$$anonymous$$d of $$anonymous$$ono => Same effect. I also downloaded the tutorial project and no change in the behavior. At first I thought, maybe I'm using a different version of the "System Event" asset, because it was under UI. But the tutorial explains that it's only one, and it's under the UI menu because this was introduced with the new UI system.
Thanks and cheers.
I also started to get this when I upgraded to Unity 2019.2.6f1 with BaseEventData. After 4 hrs of trying out things I can't get it to work. I agree with @mirage3d, I think this is a bug. Edit Add - Going back to my backups this was working for me in Unity 2019.2.4f1. But in Unity 2019.2.5f1 and Unity 2019.2.6f1 I can no longer assign callbacks in the Event Trigger.
Answer by P_Jong · Sep 23, 2019 at 12:09 PM
After I reported this bug, I was told it is a known issue and was directed to issue tracker I’d 1178687 “UNITYEVENT DRAWER NO LONGER HANDLES EVENTS IN NON-PUBLIC FIELDS CORRECTLY”
This issue says it is fixed in 2019.3 but the comments are mixed. One person said it works for them in 2019.3.0b4 and some said it does not work for them in 2019.3.0b4. For now I’m keeping my game at 2019.2.4f1 where it’s still working.
I also got that reply and was given the same issue number. $$anonymous$$y version of 2019.3.0b4 and 2019.1.14f1 work fine. I guess it's not fully resolved yet. Hope they will be able to track the reason soon and fix it once and for all.
Answer by mirage3d · Sep 20, 2019 at 02:15 PM
It's definitely a reflection thing.
Unity version 2019.1 defaults to using .Net 4.x. Whereas both 2019.2 and 2019.3 beta default back to using .Net 2.x.
The procedure works out of the box in 2019.1 ( current available version is 2019.1.14f1). Both 2019.2 and 2019.3 fail, even if I change the API compatibility back to .Net 4.x ( whether before adding the component and restarting the project, or after adding the component).
I'm guessing it's a bug. will try to file a bug report.
Unity 2019.3.0b4 does not exhibit this behavior even though the player settings were set to use "mono" and .Net Standard 2.0. Thus, the above assumption that it has to do with reflection is wrong. It is still a bug though. I have filed a bug report. Hope they fix it soon.
Your answer

Follow this Question
Related Questions
onClick Event for UI Text? 1 Answer
UI stopped working 0 Answers
How to Event tigger when drag and drop complete 1 Answer
Calling a function through EventTrigger does not loop. 1 Answer
(Solved) OnPointerUp not called 1 Answer