OnMouseOver not working.. ?

I just want to know when the mouse is over some buttons, to expand a window with some info text.

Is just simple, I have some canvas with empty objects full of texts, buttons, panels etc… some of this buttons have attached a script with this code:

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

public class GestorUpgrades : MonoBehaviour {

    public GameObject PantalletaCost;
    
    void OnMouseEnter()
    {
        Debug.Log("Enter " + gameObject.name);            
    }

    void OnMouseOver()
    {
        Debug.Log("Over " + gameObject.name);            
    }
}

But nothing happens when mouse is over or enter them. The buttons and all the canvas hicheracy is NOT in “Ignore Raycast Layer”, and i dont know why this is happenning… :frowning:

Some sugestion?
Thaanks!


Update


Hello Legend_Bacon!

Thanks for answer, i researched a little and came up with this solution.

By Inspector: Adding a EventTriger component to the button, add a “Point enter” and a “Point click” and attach the same gameobject with a script with 1 futction for each event.

     public void TheNameIWant1()
     {
         Debug.Log("Enter " + gameObject.name);            
     }
 
     public void TheNameIWant2()
     {
         Debug.Log("Clicked" + gameObject.name);            
     }

Thaanks!

Hello there,

As far as I can remember, OnMouseEnter() and OnMouseOver() only work on (old) GUI elements and objects that have a Collider component.

If you want the (new) UI to pick up these events, you need to select your button and add an “event trigger” component. Then, you can add an PointerEnter Event to it, and assign it functions to call just like with a button’s OnClick().


Hope that helps!

Cheers,

~LegendBacon

Make your class implement the UnityEngine.EventSystems.IPointerEnterHandler interface and remove the override keyword of the OnPointerEnter function.

public class GestorUpgrades : MonoBehaviour : UnityEngine.EventSystems.IPointerEnterHandler {
    public void OnPointerEnter(PointerEventData data)
     {
         Debug.Log("OnPointerEnter called.");
     }
}

The data parameter is an object containing the information sent by the EventSystem when the latter detected that the pointer entered your object.