0

I attempted to make a pingponging colored text for my splash screen. I attempted to just change the alpha value and my color turned black on run-time. When attempting to assign a color in the function, it turned white instead. Can you guys please tell me what I'm doing wrong and how I should set the color properly?

using UnityEngine;
using UnityEngine.UI;

public class LerpAlpha : MonoBehaviour {

    public float duration;
    float alpha;
    Text colorText;
    Color textColor;

    void LerpAlphaText()
    {
        float lerp = Mathf.PingPong(Time.time, duration) / duration;
        alpha = Mathf.Lerp(0.0f, 1.0f, Mathf.SmoothStep(0.0f, 1.0f, lerp));
        textColor.a = alpha;
        ///Also tried textColor = new Color(113, 75, 2, alpha); resulting in 
        ///the white text             
        colorText.color = textColor;
    }

    void Start()
    {
        colorText = GetComponent<Text>();
    }

    void Update ()
    {
        LerpAlphaText();
    }
}
EpicNicks
  • 21
  • 1
  • 7

1 Answers1

0

The RGBA components of the Color class have a range of 0-1, so Color(113, 75, 2, alpha) will return white. Either scale your components by RGBA/255.0 or simply change Color textColor; to public Color textColor; and set it in the Inspector.

Lece
  • 2,339
  • 1
  • 17
  • 21
  • Thanks so much! I thought the range was 0-255. – EpicNicks Apr 06 '18 at 03:46
  • `Color32` has range of 255 but the `.color` property you're setting is of type `Color` so although Color32 could be used, it'll require an implicit conversion which isn't free. – Lece Apr 06 '18 at 17:41