- Home /
Question by
finlay_morrison · Jul 28, 2017 at 05:58 PM ·
c#textcolor
How to change color via script
im trying to change the color of some text using the script, however it dosent see to be working, here is my code
GetComponent(Text).color = Color.green;
i am getting multiple issues because of this code, please could someone explain why.
Comment
Best Answer
Answer by Vicarian · Jul 28, 2017 at 06:07 PM
Use the generic form instead. The non-generic only returns an object of type Component, which doesn't have a color property.
Like so:
GetComponent<Text>().color = Color.green;
Answer by UnityCoach · Jul 28, 2017 at 06:07 PM
GetComponent(Text)
returns a Component
. You need to cast it back to Text type.
(Text)GetComponent(Text);
You can also simply use the generic implementation
GetComponent<Text>();
Which will return the component as the type it is given (Text).