- Home /
Color.Lerp doesn't work
So I have a GUI labels text color set to a color variable called targetColor. I have targetColor lerping from white to a red color variable, depending on a float. Dragging the float variable between 0-1 in the inspector during play mode does not lerp it. If it is less than 0.99 it will instantly go white, if it is 1 it will go red. What's going on? This follows the documentation example perfectly.
Edit: Here's my example code as requested: (I'm adjusting colorLerp in the inspector for now)
public GUIStyle healthStyle;
public Color targetColor;
public Color normalTextColor = new Color(255,255,255,255);
public Color damageTextColor = new Color(135, 0, 0, 255);
public float colorLerp = 0;
void Start(){
colorLerp = 0;
}
void Update(){
targetColor = Color.Lerp(normalTextColor, damageTextColor, colorLerp);
healthStyle.normal.textColor = targetColor;
}
void OnGUI(){
GUI.Label(new Rect(...), "Text", healthStyle);
}
Could we take a look at your code so we can get a better idea of what's going wrong?
Answer by Eric5h5 · Mar 11, 2013 at 12:41 AM
Color does not use 0-255, it uses 0.0-1.0. Color.Lerp works as expected.
Oh wow, I probably should have checked the script reference. It's weird how it doesn't give an out of bounds error though, and it even shows the colors. Thanks though, I got it working now.
Although if you want to use 0-255, you can use Color32. The only problem with Color32 is that it can't be used everywhere Color can; for example SetPixels32 doesn't have all the function overloads that SetPixels does, which limits its usefulness.