Text Colour not changing?
I'm trying to have randomly set float values change the colour of my text, but I can't seem to make it work, as far as I can tell this chunk of code should work just fine.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextColorChanger : MonoBehaviour {
public Text[] Change;
void Start() {
RainbowColour();
}
void RainbowColour() {
float rCol, gCol, bCol;
rCol = Random.Range(0, 255);
gCol = Random.Range(0, 255);
bCol = Random.Range(0, 255);
Change[0].color = new Color(rCol, gCol, bCol);
Change[1].color = new Color(rCol, gCol, bCol);
Debug.Log("rCol: " + rCol + " gCol: " + gCol + " bCol: " + bCol);
}
}
Answer by fmtrick · Nov 21, 2016 at 07:21 AM
set those Col variables to integers since you're using whole numbers. That may help.
Answer by brunocoimbra · Nov 21, 2016 at 12:11 PM
Your problem is with "while (true)"! If you want it to be updated, then use the Update method, the ONLY way to use "while (true)" in Unity is in a Coroutine.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextColorChanger : MonoBehaviour {
public Text[] Change;
void Start() {
StartCoroutine(RainbowColour());
}
IEnumerator RainbowColour() {
while (true) {
float rCol, gCol, bCol;
rCol = Random.Range(0, 255);
gCol = Random.Range(0, 255);
bCol = Random.Range(0, 255);
Change[0].color = new Color(rCol, gCol, bCol);
Change[1].color = new Color(rCol, gCol, bCol);
Debug.Log("rCol: " + rCol + " gCol: " + gCol + " bCol: " + bCol);
yield return new WaitForSeconds(0.5f);
}
}
}
Here is a little explanation about how to use Coroutines:
Coroutines are like a method, but that follows a routine (!), executing piece by piece after meeting the desired condition;
It should be started with "StartCoroutine(MyCoroutine());";
The coroutine should be a Method with IEnumerator as it's return (not "void");
Inside it, you should use "yield return" followed by the condition that it should wait to continue it's execution (in this case, we say that we want to wait half a second). If you use "yield return null;" if will just wait pass 1 frame. To stop the Coroutine execution you could use "yield break;"
If you would like to undestand more about how to use coroutines, take a look around, there is a plenty of answer already about it.
The while statement wasn't actually supposed to be there, forgot to remove it from the question, with it removed the colour should change once, but it remains white, and all the text colour values stay at 255.
About that, Color(r, g, b) takes arguments from 0 to 1. To use the 255 format, use Color32 ins$$anonymous$$d.
I still seem to be having issues, at the point I just want to assume I'm going crazy and missing something really simple, the Color32 will change constantly, but the text will stay un-affected.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextColorChanger : $$anonymous$$onoBehaviour {
public Text[] Change;
public Color32 textColor32;
void Awake() {
Change[0] = this.GetComponent<Text>();
}
void Update() {
RainbowColour();
}
void RainbowColour() {
textColor32 = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 1);
Change[0].color = textColor32;
}
}
Your answer
Follow this Question
Related Questions
how do i reference to a text objects text in scripting? 2 Answers
How To Load TTF Font From External File 0 Answers
How to link up instantiated text in list to allow buttons to adjust number shown, c# 1 Answer
Removing an item from Inventory and updating item count on HUD 0 Answers
How to print variable? (C#) 1 Answer