- Home /
Trying to change text colour based on random values.
I'm not sure why this isn't working, I'm trying to change the text colour based off a random value but the text remains the same colour on start.
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);
}
}
I would reccomend, converting this function into a for-loop, since your only calling it in Start.
for (int i = 0; i < Change.Length; i++){
rCol = Random.Range(0, 255);
gCol = Random.Range(0, 255);
bCol = Random.Range(0, 255);
Change[i].color = new Color(rCol, gCol, bCol);
Debug.Log("Text " + i + " color = rCol: " + rCol + " gCol: " + gCol + " bCol: " + bCol);
}
This will be able to tell you the random color its trying to choose, for each text... If they all return 0, then you know its a different problem, but if they are all returning different values that is not 0, then you could try something like:
for (int i = 0; i < Change.Length; i++){
rCol = Random.Range(0, 255);
gCol = Random.Range(0, 255);
bCol = Random.Range(0, 255);
Color myNewColor = new Color(rCol, gCol, bCol);
Change[i].color = myNewColor;
Debug.Log("Text " + i + " color = " + myNewColor);
}
And see if you get the same problem.
Your answer
Follow this Question
Related Questions
DIALOGUE BRANCH CREATION? 1 Answer
How to display images on top of TileMap during runtime? 0 Answers
Create multiple math problems with arrays? Help with arrays 1 Answer
Accessing text component. 2 Answers
Is it possible to make textMeshProGui text to toggle with the unity editor gizmo's button? 0 Answers