Check if mouse is over UI

How do I check if the cursor is hovering over a ui image?

I’ve tried EventSystem.IsPointerOverGameObject, but I couldn’t get it to work. Is there another way? or can someone guide me through how to use the EventSystem.IsPointerOverGameObject?

Thank you.

Did you try using OnPointerEnter/OnPointerExit?

https://docs.unity3d.com/ScriptReference/UI.Selectable.html

I figured out why I couldn’t use the OnPointerEnter/Exit.

In my FirstPersonController I had Cursor.lockState = CursorLockMode.Locked, and that’s why it couldn’t register when the cursor was over the ui image. I fixed it now.

I did a script that you can add to every Canvas. Then you can reach the static bool MouseInputUIBlocker.BlockedByUI from anywhere to see if the mouse is over UI or not.

This is the same as LarsBlack’s setup but it’s automatic.

I know it’s dirty with the static bool I only did this as a quick fix but it might help someone:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;

[RequireComponent(typeof(EventTrigger))]
public class MouseInputUIBlocker : MonoBehaviour
{
    public static bool BlockedByUI;
    private EventTrigger eventTrigger;

    private void Start()
    {
        eventTrigger = GetComponent<EventTrigger>();
        if(eventTrigger != null)
        {
            EventTrigger.Entry enterUIEntry = new EventTrigger.Entry();
            // Pointer Enter
            enterUIEntry.eventID = EventTriggerType.PointerEnter;
            enterUIEntry.callback.AddListener((eventData) => { EnterUI(); });
            eventTrigger.triggers.Add(enterUIEntry);

            //Pointer Exit
            EventTrigger.Entry exitUIEntry = new EventTrigger.Entry();
            exitUIEntry.eventID = EventTriggerType.PointerExit;
            exitUIEntry.callback.AddListener((eventData) => { ExitUI(); });
            eventTrigger.triggers.Add(exitUIEntry);
        }
    }

    public void EnterUI()
    {
        BlockedByUI = true;
    }
    public void ExitUI()
    {
        BlockedByUI = false;
    }

}

@MrMelonPie You can try using the Event Trigger Component. Redirect to... title of new-page The Event trigger allows you to call functions from scripts on the object.
!

[1]
In this image I have the Event Trigger call a void that changes a Boolean (the Boolean is called mouseover it tells the script when the mouse is over the game object) when the mouse is over it.

  [1]: file:///C:/Users/willm/Pictures/Screenshots/Screenshot%20(59).png