Callback.AddListener dosen't add Listener to EventTrigger
Hey,
I'm trying to add an event type to an event trigger via script. In my main class LoadLevel I'm calling the function SetClickEvent() in the class LoadParts (removed unnecessary parts and transformed to pseudocode)
public class LoadLevel : MonoBehaviour {
void SpawnParts()
{
loop()
{
loadP.SetClickEvent();
}
}
With SetClickEvent() I create a new EventTrigger.Entry() with the eventID: PointerDown and the function that should be called partSprite.GetComponent<RotateP>().RotatePart((PointerEventData)data);
Then I'm adding this entry to my EventTrigger component.
public class LoadParts {
public GameObject partSprite;
public void SetClickEvent()
{
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener((data) => {
partSprite.GetComponent<RotateP>().RotatePart((PointerEventData)data);
});
partSprite.GetComponent<EventTrigger>().triggers.Add(entry);
Debug.Log("Added event");
}
}
When I test the code I can see that the entry with the eventID gets added to the EventTrigger but without the listener: What am I doing wrong? Btw. I'm getting no errors and the code already worked when the LoadLevel and LoadParts class were merged together.
Thanks in advanced,
Tobias
Have the same problem. It doesn't show up and after scene reload even the empty event block disappears.
Answer by smosh92 · Jan 12, 2019 at 10:20 AM
It's a bug in Unity Editor. It won't show any function assigned in the inspector but it will work. Give it a try.
Your answer
Follow this Question
Related Questions
Question about triggering an event (Extremely Novice to Programming) 1 Answer
Don't know how to access UnityEvent listener 1 Answer
How to add a editing option to variables in a script in Inspector? 0 Answers
How to create buttons with script? 0 Answers
Side-scrolling Enemy AI not shooting in the right direction 0 Answers