Getting the current item my mouse is hovering over..

SOLVED

Hey guys, my goal is to be able to drag an item to a specific place and dependent on where I end up dragging the item, drop it or move it to a different slot in my inventory. (FYI, I can’t use raycast because I need my canvas to “block raycasts”). This is because if I click a location to move I’ll click through the inventory since I use raycasts to navigate…

I found this thread and it’s essentially the same thing I want to achieve:

I can narrow down if I’m hovered over a UI element or if I’m not by using eventSystem.IsPointerOverGameObject()… but I need to find the actual gameobject I’m hovered over so I can get the tag.

I’m getting a null reference exception:

on this line:

        _instance.GetPointerData(pointerId, out eventData, true);

I simply want a way to get the current object that my mouse is hovered over in my UI!

Here’s my eventsystem as well - I tried checking the force module active but it didn’t do anything: Screenshot - 7204fb5669d23307db355a620612a9aa - Gyazo
As you can see, I get the exception but don’t print out the " Debug.Log(test.hovered);" on the following line…

Here’s the code I wrote + the extendedstandaloneinputmodule:

using UnityEngine.EventSystems;

public class ExtendedStandaloneInputModule : StandaloneInputModule
{
    public static PointerEventData GetPointerEventData(int pointerId = -1)
    {
        PointerEventData eventData;
        _instance.GetPointerData(pointerId, out eventData, true);
        return eventData;
    }

    private static ExtendedStandaloneInputModule _instance;

    protected override void Awake()
    {
        base.Awake();
        _instance = this;
    }
}
 eventSystem = GameObject.FindObjectOfType<EventSystem>();
        //uiRaycast = eventSystem.GetComponent<GraphicRaycaster>();

        //List<RaycastResult> results = new List<RaycastResult>();
        //uiRaycast.Raycast(eventData, results);

        //results has all of the canvas objects

        if(eventSystem.IsPointerOverGameObject())
        {
            PointerEventData test = ExtendedStandaloneInputModule.GetPointerEventData();
            Debug.Log(test.hovered);

            if (eventSystem.currentSelectedGameObject.GetComponent<Collider>().tag.Equals("Minimap"))//eventSystem.gameObject.GetComponent<Collider>().tag.Equals("Minimap"))
            {
                Debug.Log("To minimap");
            }
            //inventory, stats or minimap
        }
1 Like

I’m such an idiot - I didn’t add the script to my eventsystem…

2 Likes

How did you add it to the event system?

His script implements Standalone Input module, a script usually automatically added on an object called “EventSystem” when you add a canvas to your scene. I imagine he just needed to remove the default components and add his derived component.