- Home /
UI Text.color not assigning?
So I made method to remotely spawn damage numbers, and I wanted to make it so you could change the color as well. However, the color never seems to change from gray. Whats weird is that when I changed the prefab's text color, it was set to gray still when I ran the game, so I am quite confused. Any idea why the color isn't being set?
{
public Animator anim;
private Text damageText;
void Start () {
AnimatorClipInfo[] clipInfo = anim.GetCurrentAnimatorClipInfo(0);
Destroy(gameObject, clipInfo[0].clip.length - 0.1f);
damageText = anim.GetComponent<Text>();
}
public void SetText(string text)
{
anim.GetComponent<Text>().text = text;
}
public void SetColor(string color)
{
damageText = anim.GetComponent<Text>();
if (color == "Gray")
{
damageText.color = new Color(0.5f, 0.5f, 0.5f, 1f);
}
else if (color == "Red")
{
damageText.color = Color.red;
}
else
{
Debug.LogError(color + " is not a valid color!");
}
}
}
First thing I would check is if the value you are passing to SetColor(string color) matches the case that the script is expecting (e.g. "red" ins$$anonymous$$d of "Red"). If this is working correctly the next thing I would check is that the animator component is on the same object as the text component.
Are you getting errors or warnings in the console when it runs?
Answer by s_awali · Nov 08, 2018 at 02:00 PM
I see you have an animator on the GameObject. If your animator has a single animation that modify the color of your Text, even on 1 single frame, then this property is "locked", and will always be overriden by the animation, no matter what you do in code.
Oh! Yes that would explain why. It always plays an animation that makes it fade out when it gets instantiated. I wonder what the work around for this would be? Could I maybe make two animation clips?
Nope, if 1 animation set the color, even if the animation is not playing the value is locked. You should ins$$anonymous$$d add a CanvasGroup component to the parent of your button, and change the alpha value. This will fade every child.
Answer by swanijam · Nov 08, 2018 at 04:31 PM
I'd recommend doing the animated alpha fade in a coroutine, on a script. This way you can apply it to a specific Text without creating extra canvas elements, you don't have to animate it with keyframes, and you can still very easily change the curve it uses to interpolate. The coroutine stops after the duration and releases control of the alpha value forever. Here's the code I use frequently to do this:
public Text text;
float fadeDuration;
Color maximumOpacityColor, minimumOpacityColor;
public AnimationCurve animationCurve;
private void Start()
{
StartCoroutine(InterpolateColor(maximumOpacityColor, minimumOpacityColor, fadeDuration));
}
private IEnumerator InterpolateColor(Color minColor, Color maxColor, float _time) {
float currTime = 0f;
while (currTime < _time) {
currTime += Time.deltaTime;
float lerpVal = Mathf.InverseLerp(0f, _time, currTime);
text.color = Color.Lerp(minColor, maxColor, animationCurve.Evaluate(lerpVal));
yield return new WaitForEndOfFrame();
}
}