I'm trying to change the .lb.color to cycle through the color spectrum . I have a UILabel in project that instantiates as a score (+20), when an enemy is killed.. here's the script I added to that gameobject:
using UnityEngine;
using System.Collections;
public class changecolor : MonoBehaviour
{
public UILabel _lb;
public Color32[] colors;
public int currentIndex = 0;
private int nextIndex;
public float changeColourTime = 2.0f;
private float lastChange = 0.0f;
private float timer = 0.0f;
void Start()
{
if (colors == null || colors.Length < 2)
Debug.Log("Need to setup colors array in inspector");
nextIndex = (currentIndex + 1) % colors.Length;
}
void Update()
{
timer += Time.deltaTime;
if (timer > changeColourTime)
{
currentIndex = (currentIndex + 1) % colors.Length;
nextIndex = (currentIndex + 1) % colors.Length;
timer = 0.0f;
}
_lb.color = Color32.Lerp(colors[currentIndex], colors[nextIndex], timer / changeColourTime);
}
}
But it doesn't do anything, it actually makes the text disappear.
EDIT: After reading through posts regarding UI Label colors , it's fixed .
I kept reading about how I'm suppose to only use Color32 and I changed the _lb.color = Color.Lerp to _lb.color = Color32.Lerp and the text disappeared, but then I realized I didn't change the array of colors up top to Color32. Now it works like a charm. Code is edited to fixed code if anyone wants to use it , just make sure to set your colors again in inspector because changing from color to color32 changes them to pink for some reason.. it makes a great rainbow effect for text.