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?