Can't get EventTriggers to work

I have a Canvas with a RawImage named “tal_ImgFireball”. In my Talents.cs class, which is attached to a separate object, I have:

using UnityEngine.EventSystems;

EventTrigger.Entry eventtype; // I did this just to make sure eventtype losing scope wasn't the problem. Declaring it here makes sure it isn't deleted.

    void Start()
    {
        GameObject FBtalent = GameObject.Find("tal_ImgFireball"); // the raw image in the canvas
        FBtalent.AddComponent<EventTrigger>();

        //mouse enter
        eventtype = new EventTrigger.Entry();
        eventtype.eventID = EventTriggerType.PointerEnter; // I have tried PointerClick as well.
        eventtype.callback.AddListener((eventData) => { FBeventIn(); });
        FBtalent.GetComponent<EventTrigger>().triggers.Add(eventtype);
    }

    private void FBeventIn()
    {
        Debug.Log("in"); // No matter what I do, I can't get this to ever log.
    }

There are some variations of this code that I have also tried, but have not been able to get them to work either.

I saw that using IPointerEnterHandler was an option. The individual who wrote this code said to attach it in a script directly to the Canvas and that it would work for any children UI elements within the Canvas. This also did not work.

public class CanvasEvents : MonoBehaviour, IPointerEnterHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Name: " + eventData.pointerCurrentRaycast.gameObject.name);
    }
}

I saw a comment about how you should make sure you have Raycast target selected on the object you are trying to listen to events for, so I made sure of that.

I also saw where with using 3D, non-UI objects that you have to use a collider. I tried this anyway simply because I was out of options, but it didn’t work either.

I also tried to do this in a fresh scene, thinking maybe something I had done along the way was messing this up, but I also could not get this to work.

Usually I figure it out the moment I bother to make a thread about it, so fingers crossed. Any ideas?

One useful thing to note: it’s fine that eventype goes out of scope. The object won’t get destructed until all references to it are lost – and the EventTrigger component will have a reference!

Anyway, excluding this particular problem, is UI interaction working in general? Can you click on buttons and whatnot?

I’ve never seen it done this way.

The two suspicious things to me are:

  • line 11 calling new on a Unity-ish object (might be legit, this might just be a POCO class)
  • line 14 adding it in

Does it work if you just manually add the EventTrigger to the GameObject, give yourself a public reference to EventTrigger, and from there add a listener to it??

ALSO, FYI, remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: Regarding GameObject.Find · UnityTipsRedux

More information: Why cant i find the other objects?

So, after adding an actual button to the Canvas and making sure the “Pressed color” is something really noticeable, it does not look like it is working at all.

This might be relevant. The EventSystem gets created automatically when adding a canvas, but you could’ve removed it (or you’re doing all of your work in a prefab!)

Yep. I just saw in a new file that the UI works with the input system attached. I knew I needed the event system, but had removed the input thing in my main project thinking I didn’t need it. By the looks of it, you’d think it would only apply to keyboard movement type things. I assumed it was something to do with the “new input” vs “legacy input” system. But nope, it’s for UI as well. Now we know.

It does, indeed, have something to do with the “new” and “old” input systems! You get a different component alongside the EventSystem depending on which system is in use.

The input system has to translate your button presses and mouse clicks into events that make sense to the UI.

I was perplexed at first as well – it’s this magical component you have to have or everything breaks :stuck_out_tongue: