Author Topic: Cycle UI Label Color through color spectrum?  (Read 99 times)

yaezah

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Cycle UI Label Color through color spectrum?
« on: October 11, 2017, 04:34:33 PM »
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:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class changecolor : MonoBehaviour
  5. {
  6.     public UILabel _lb;
  7.  
  8.     public Color32[] colors;
  9.  
  10.     public int currentIndex = 0;
  11.     private int nextIndex;
  12.  
  13.     public float changeColourTime = 2.0f;
  14.  
  15.     private float lastChange = 0.0f;
  16.     private float timer = 0.0f;
  17.  
  18.     void Start()
  19.     {
  20.         if (colors == null || colors.Length < 2)
  21.             Debug.Log("Need to setup colors array in inspector");
  22.  
  23.         nextIndex = (currentIndex + 1) % colors.Length;
  24.     }
  25.  
  26.     void Update()
  27.     {
  28.  
  29.         timer += Time.deltaTime;
  30.  
  31.         if (timer > changeColourTime)
  32.         {
  33.             currentIndex = (currentIndex + 1) % colors.Length;
  34.             nextIndex = (currentIndex + 1) % colors.Length;
  35.             timer = 0.0f;
  36.  
  37.         }
  38.      
  39.         _lb.color = Color32.Lerp(colors[currentIndex], colors[nextIndex], timer / changeColourTime);
  40.     }
  41. }

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.
« Last Edit: October 11, 2017, 05:12:06 PM by yaezah »