Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to change the Text color on a button when you press it?

Discussion in 'Scripting' started by qiqette, Feb 10, 2016.

  1. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    I have no Idea how I can do it, or even if it has to be on Scripting threads :S
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI; // needed to access the new UI classes and functions
    4. using System.Collections;
    5.  
    6. public class TestScript : MonoBehaviour
    7. {
    8.     public Text t;
    9.  
    10.    public void ChangeColour()
    11.     {
    12.         t.color = Color.red;
    13.     }
    14.  
    15. }
    16.  
    call ChangeColour in the buttons OnClick event, the script needs to have the relevant text field dragged into it's inspector slot.

    how to wire up the UI buttons: https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button?playlist=17111
     
    qiqette and karl_jones like this.