Change Text Color

Hello everyone!

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…

I wanted to do that with C# script.

Does anyone of you know how this works?

Thank you in advance!

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
    }
}

Hope this helpes!

3 Likes

you want to save the text in a public Text name;

Just click the Gizmos button top of the scene view

@lasdoo5 lol thanks