- Home /
The question is answered, right answer was accepted
Change UI button text during hover.
Hi,
Is there a way to change the text that is on the UI button when it is hovered over? I understand that I could use sprites for my button, but as I just want to change a small amount of text I was hoping there might be a way of doing it without creating sprites.
Thanks,
Answer by Kiwasi · Dec 29, 2014 at 07:32 PM
Sure. There are multiple options. One is to change the value of Text.text. Here is a script to get you started. Attach it to the button.
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ChangeText : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
private Text myText;
void Start (){
myText = GetComponentInChildren<Text>();
}
public void OnPointerEnter (PointerEventData eventData)
{
myText.text = "Hovering";
}
public void OnPointerExit (PointerEventData eventData)
{
myText.text = "Not Hovering";
}
}
Note you can also do this using animated transitions, or by adding event triggers, or by completely swapping the text out.
Follow this Question
Related Questions
Hovering usernames with smartfox? 1 Answer
How would you make objects appear when the mouse goes over them? 1 Answer
cant change standard shader emission color? 0 Answers
Hovering Car Script 2 Answers