Issues with OnMouseOver() not working

(I know there are many threads about this, but none seem to fix my issue)

I’ve been trying to get a UI Button component to make another text component change when highlighted.

Even though I have a 2D Rigidbody and I made sure to make it a trigger, I can’t get my code to enter the OnMouseOver() event. I don’t have a background and my Button is set to “Raycast Target”.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class mouseOverController : MonoBehaviour
{
    public Text descriptionText;
    public string description;

    void Start()
    {
        descriptionText = descriptionText.GetComponent<Text>();
    }

    public void OnMouseOver()
    {
        Debug.Log("Here"); //This is never logging
        descriptionText.text = description;
    }
}

This script is attached to each Button that I want to change my text component.

Any ideas? All suggestions you can come up with are welcome!

What you could do is implement the appropriate interfaces IPointerEnterHandler, IPointerExitHandler .

Your adjusted code would need to be something like :

public class mouseOverController : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     public Text descriptionText;
     public string description;
 
     void Start()
     {
         descriptionText = descriptionText.GetComponent<Text>();
     }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Here"); //This is never logging
         descriptionText.text = description;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Mouse exit");
    }
 }
  1. Idk why it doesn’t mention anywhere that OnMouse***() functions DO NOT work on UI elements, but they don’t. You have to either use the EventTrigger component (PointerEnter event) or you can do what codeelemental said and implement the corresponding interfaces.
  2. Also, you are probably looking for OnMouseEnter, as OnMouseOver is called every frame.
  3. Another thought is, it seems you are trying to make a hint text. You might want to try and change the button transition type to animation instead of color, and enable the text object in the “highlighted” animation clip (you must also make the text a child of the button). You could even do a nice smooth expansion of a chat box or something.