Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Can't get EventTriggers to work

Discussion in 'Scripting' started by Bluelattice5555, Jan 23, 2023.

  1. Bluelattice5555

    Bluelattice5555

    Joined:
    May 8, 2020
    Posts:
    34
    I have a Canvas with a RawImage named "tal_ImgFireball". In my Talents.cs class, which is attached to a separate object, I have:

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2.  
    3. 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.
    4.  
    5.     void Start()
    6.     {
    7.         GameObject FBtalent = GameObject.Find("tal_ImgFireball"); // the raw image in the canvas
    8.         FBtalent.AddComponent<EventTrigger>();
    9.  
    10.         //mouse enter
    11.         eventtype = new EventTrigger.Entry();
    12.         eventtype.eventID = EventTriggerType.PointerEnter; // I have tried PointerClick as well.
    13.         eventtype.callback.AddListener((eventData) => { FBeventIn(); });
    14.         FBtalent.GetComponent<EventTrigger>().triggers.Add(eventtype);
    15.     }
    16.  
    17.     private void FBeventIn()
    18.     {
    19.         Debug.Log("in"); // No matter what I do, I can't get this to ever log.
    20.     }

    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.

    Code (CSharp):
    1. public class CanvasEvents : MonoBehaviour, IPointerEnterHandler
    2. {
    3.     public void OnPointerEnter(PointerEventData eventData)
    4.     {
    5.         Debug.Log("Name: " + eventData.pointerCurrentRaycast.gameObject.name);
    6.     }
    7. }
    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?
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    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?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    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: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066
     
  4. Bluelattice5555

    Bluelattice5555

    Joined:
    May 8, 2020
    Posts:
    34
    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.
     
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    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!)
     
  6. Bluelattice5555

    Bluelattice5555

    Joined:
    May 8, 2020
    Posts:
    34
    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.
     
  7. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    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 :p