OnMouseOver UI Button c#

How to detect in script, if the mouse is over a script-created button? If any component is needed, how can you create the object and set the component’s settings in the right way?

You don’t use OnMouseOver for UI elements, use IPointerEnterHandler Then attach script to game object that you want to implement it on.

example:

using UnityEngine.EventSystems;

public class MyClass: MonoBehaviour, IPointerEnterHandler{
	
    public void OnPointerEnter(PointerEventData eventData)
    {
        //do stuff
    }

I’m building on Flaring-Afro’s answer. IPointerEnterHandler works but is not the same as OnMouseOver. To get this behaviour you (unfortunately) also need IPointerExitHandler, a private bool and the Update function such as:

public class UIElement : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    private bool mouse_over = false;

    void Update()
    {
        if (mouse_over)
        {
            Debug.Log("Mouse Over");
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        mouse_over = true;
        Debug.Log("Mouse enter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        mouse_over = false;
        Debug.Log("Mouse exit");
    }
}

Work like a charm