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!
did you mean to say made sure NOT to make it a trigger? because in the documentation it specifically says it will not work on triggers unless you have physics.querieshittriggers set to true.
Answer by CodeElemental · Jul 19, 2016 at 07:44 AM
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");
}
}
Thank you! I used this to implement tooltips for UI elements.
Been scouring the web for an easy solution for the last 2 hours. Finally something simple and easy. THAN$$anonymous$$ YOU!
Thank you! Is there a reason why the documentation for $$anonymous$$onoBehaviour.On$$anonymous$$ouseEnter() doesn't implement the interfaces?
Answer by moment_um · Jul 19, 2018 at 11:33 PM
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.
Also, you are probably looking for OnMouseEnter, as OnMouseOver is called every frame.
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.