I have a text element in my scene and I want it to change color when i go over it with my mouse. I don’t seem to find a solution because in every video I watched they use a 3D object or a button…
To start with you should add a new script to your text. This code changes the colour when entering and exiting the text object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // This is so that it should find the Text component
using UnityEngine.Events; // This is so that you can extend the pointer handlers
using UnityEngine.EventSystems; // This is so that you can extend the pointer handlers
public class ColourChanger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { // Extends the pointer handlers
// Test for enter and exit:
public void OnPointerEnter(PointerEventData eventData) {
GetComponent<Text>().color = Color.gray; // Changes the colour of the text
}
public void OnPointerExit(PointerEventData eventData) {
GetComponent<Text>().color = Color.black; // Changes the colour of the text
}
}