- Home /
UI text color not assigning correctly
Okay, so after looking at a few things, I thought I'd start scripting some text fading for an intro.
I assign objColour the UI text's colour (using gameObject.GetComponent), and then in the Fade routine, I immediately set it to red (it starts white, the red is for testing purposes). However, the text will not change colour unless the color is directly set from GetComponent.
See this .cs file - the current state of it will not change the text, but if the commented-out line is uncommented, it will change the text.
Reason I want to use objColour is because writing gameObject.GetComponent().color = Color(...); every time I wish to set it makes things far less readable.
Unity version is 5.2
Using GetComponent is also bad for performance reasons. Constantly having the game search through the object's components is slow, so it should only be used to store references once, then use that reference later. However, as SlateNeon says in their answer, you're not storing the field reference at the top of your Start() method, but just copying the current color that's there.
Follow SlateNeon's advice to store the Text reference and change the color using that, and you should get what you want.
Answer by SlateNeon · Oct 27, 2015 at 12:08 PM
This happens because when you do objColour = GetC().color, you get the color of the text, not the field reference to it. Right now you get a reference to Color object, later you simply change that object to another one, but the text color is still referencing the original color.
To do what you want you should do obj = GetComponent()-text- and then in Fade obj.color = ...
void Start(){
obj = GetComponent<Text>();
...coroutine...
}
IEnumrator Fade(...){
obj.color = Color.red;
}
Something like that
$$anonymous$$any thanks, I see what you mean! I was thinking almost unconditionally that it would return a value by reference, and this clears it up. Works now too, thank you very much.
Answer by Ashkan_gc · Oct 27, 2015 at 05:23 PM
Color is a struct and in C# structs are passed by value. It means when you get the color of the Text component a copy of the Color struct with the same values of Text.color will be created for you. Then when you change that color, your color will be modified which is a copy and the original Text.color will not be modified. If it was a class (classes are pass by reference) a reference of the Text.color would be provided to you and your modifications would be reflected on Text.color, in order to change the color of the Text you need to store the Text component in a variable and modify its color. var text = gameObject.GetComponent(); text.color=yourDesiredColor;
To make the fade beautiful , other than using Color.Lerp you can use an AnimationCurve and use its Evaluate method to get a value between 0 and 1 for the Lerp based on its curve. then you can play with the curve's shape in editor if AnimationCurve is a public field and visible in inspector. Otherwise you have to change the weight of the Lerp using a formula in code, linear or square or ...
Thanks for clearing up that structs are passed by value in C# - haven't ever used structs much, so that took me by surprise. Also cheers for the AnimationCurve suggestion, that ought to make things look very nice indeed, and I hadn't thought of it.