Reputation: 63
I have a GameObject embedded inside my Canvas that has a "Text (Script)" Component. I wish to alter the color.a
attribute during runtime of that element. Does anybody have an idea of how to do it? I can't seem to access it with any GetComponent<Type> ()
command.
Upvotes: 2
Views: 49830
Reputation: 1
You can use color attribute to provide color for the text.
It can be done by two ways -> using the color static attributes and color class constructor.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ColorDemo : MonoBehaviour {
[SerializeField]
Text infoText;
void Start () {
infoText.text = "Hello World";
infoText.color = Color.red; //Red color using Color class
//Red color using hex value. (255,0,0) => 255/255, 0/255, 0/255) => (1,0,0)
infoText.color = new Color(1f, 0f, 0f);
// Color with opacity value. 1 means opaque and 0 means transparent.
infoText.color = new Color(1f, 0f, 0f, 0.5f);
}
}
Upvotes: 0
Reputation: 47
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";
Upvotes: 4
Reputation: 763
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);
}
Upvotes: 7
Reputation: 2842
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;
}
}
Upvotes: 2