- Home /
How to change color of animated Text component
So I have a text component that is animated to scale up and translate around for a bit. I would like to change it's color through script, but it seems that changing the color on the text component doesn't actually have any effect.
I want to instantiate a prefab and play this animation with a variety of colors. I'd also like to animate the alpha of the color on the text component if that is possible.
I do have write defaults set to false in the animator.
The code that triggers the text to instantiate and play the animation:
public void TriggerText(string text, Color col)
{
Vector3 eulers = new Vector3(0, 180, 0);
Quaternion rot = Quaternion.Euler (eulers);
GameObject texteffect = (GameObject)Instantiate (floatingGraphic, Vector3.zero, Quaternion.identity);
texteffect.transform.SetParent(unitCan.transform); //sets parent to a world space canvas
texteffect.transform.localScale = Vector3.one;
texteffect.transform.localPosition = Vector3.zero;
texteffect.transform.localRotation = rot;
Animator anim = floatingGraphic.gameObject.GetComponent<Animator> ();
texteffect.GetComponent<Text>().text = text;
texteffect.GetComponent<Text> ().color = col;
Debug.Log (col + "/" + texteffect.GetComponent<Text> ().color);
anim.Play ("Text Spring in fade out");
}
The line that calls the method:
focus_Unit.TriggerText("Turn!", Color.green);
I just tried to add an animator on a text component, animating the color works perfectly fine.
I would like to change it's color through script
I instantiate the prefab, change the color, play the animation. The animator seems to be changing the color back.
The animation doesn't animate the color property at all, though I would love to just animate the alpha, and still set the color through code.
Hmm didn't catch that sorry. Well maybe put your code and screenshot then so we could help? I do some color change in one of my text. Here is how it looks like: (C#)
Color color = timerText.color;
color.b -= Time.deltaTime * 0.2f;
color.g -= Time.deltaTime * 0.2f;
timerText.color = color;
Where timerText is a Text and it's located in an Update but it can be in any kind of loop.
Edited OP to add code and a link to a video. Thanks again for your assistance!
It seems if I add the prefab in edit mode and change the color in the inspector, it works fine. I dunno what is in the code that might change it back only if I instantiate it during runtime.
Answer by LMan · May 26, 2015 at 02:26 PM
Deleted the Animator Controller, made a new one, added the same animation clips back in and it worked! The color change through script sticks. Almost definitely a bug, but unfortunately one I can't reproduce.
This bug occurred in Unity 5.0.2f1 Personal, running Windows 7.
Answer by barbe63 · May 25, 2015 at 01:19 PM
I think I have an idea of what's going on. It's probably due to your animator who is overriding the color of your text. I don't want to say mistakes but I think remembering I encountered some case where the animator was overriding my color values even if I had no keys with color.
Try first to check if disabling the animator solve the issue, then try to see if a key is related to the renderer it may be the problem. I don't know how you could past that issue if you need the renderer keys or worst if it is indeed due to the animator and no renderer keys are present.
Hmm at one time the animation did have keys for the color... although I deleted them. I'll try re-doing the animation from scratch and avoid the color property entirely And see if that works. I think you are probably right.
Alright, the animator is definitely the culprit. disabling the animator leaves the color intact. I made a new animation that just translates and scales- no color keys at any time. Still resets the color. I found that changing the culling mode on the animator to anything other than "always animate" leaves the color, but also culls out any translation in the animation.
I bet I could workaround by re-setting the color in late update every frame to counteract the animator, but that's really disappointing if I have to resort to that.
I think it deserves a bug report. That shouldn't affect the color if no rendering keys are presents at all.
Tried to reproduce in a clean project... it works. The universe strikes again!
Your answer
Follow this Question
Related Questions
What's your equivalent of old GUIStyle ? 0 Answers
How to measure the width of a string? 0 Answers
How to use special symbols 1 Answer