unity3d – Change color of text during runtime (Unity)?

unity3d – Change color of text during runtime (Unity)?

Well if you want to change R,G,B or A components of the color of the text you can do it this way:

Public Text text;
float r=0.2f,g=0.3f,b=0.7f,a=0.6f;

void Start()
{
  text=gameobject.GetComponent<Text>();
  text.color= new Color(r,g,b,a);
}
public Text myText;

Attach this to the UI component of text in Hierarchy

myText.color = Color.green;
myText.text = Enter anything, will display in UI Text;

unity3d – Change color of text during runtime (Unity)?

As far as I know you have to assign new Color to text.color. You can make your own color to assign or use one of the standard colors:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class test : MonoBehaviour {

public Text text;

void Start () 

    {
       text = gameObject.GetComponent<Text> ();
       text.color = Color.white;
    }
}

Leave a Reply

Your email address will not be published.