- Home /
Add EventTriggers through code
Hello!
So I am trying to add some EventTrigger through code but the functions won't get added to the triggers. The PointerEnter and PointerExit gets added to the EventTriggers but they are empty. Any ideas why this isn't working?
Here is the code:
public void InitSpellUI()
{
for(int i = 0; i < spellButtons.Length; i++)
{
EventTrigger eT = spellButtons[i].gameObject.AddComponent<EventTrigger>() as EventTrigger;
EventTrigger.Entry entryEnter = new EventTrigger.Entry();
entryEnter.eventID = EventTriggerType.PointerEnter;
entryEnter.callback.AddListener((eventData) => { this.ToggleToolTipSpell( spellButtons[i].gameObject.GetComponent<Tooltip>().tooltip); });
EventTrigger.Entry entryLeave = new EventTrigger.Entry();
entryLeave.eventID = EventTriggerType.PointerExit;
entryLeave.callback.AddListener((eventData) => { this.ToggleToolTipSpell( spellButtons[i].gameObject.GetComponent<Tooltip>().tooltip); });
eT.triggers.Add(entryEnter);
eT.triggers.Add(entryLeave);
}
}
public void ToggleToolTipSpell(string tooltip)
{
spellTooltipTextObject.GetComponentsInChildren<Transform>()[0].GetComponent<Text>().text = tooltip;
}
This code is attached to a separate object from those that the listeners are applied to, but I want the listeners to call a function on this object.
//Simon
Your answer
![](https://koobas.hobune.stream/wayback/20220612085851im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating a custom slider - should I be invoking a UnityEvent onValueChanged? 0 Answers
UI Buttons to work continuously when pressed and held for sometime 1 Answer
Child UI is backfiring 0 Answers
Use Event Triggers or something similar on non-UI objects? 0 Answers
How can I Take axis and turn wheel using pointer event from UI button for mobile platform..? 0 Answers