G
G
Gregory2562021-07-21 18:21:30
Unity
Gregory256, 2021-07-21 18:21:30

How to change text color?

Good day, please tell me how you can change the color of the text? The goal is to get different colors for the text randomly, while from a given range. If I set only one color, everything works, but nothing happens with the array.

public TextMeshProUGUI targetCountText;
    public List<Color> colorList; 

void Update()
    {
        ChangeColors();
    }
    void ChangeColors()
    {
        int index = Random.Range(0,colorList.Count);
        colorList[0] = Color.black;
        colorList[1] = Color.blue;
        colorList[2] = Color.gray;
        colorList[3] = Color.green;
        targetCountText.color =  colorList[index];
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2021-07-21
@Gregory256

That's cool, but does it even work? Instead of an array, you create a leaf, but you don't initialize it. Moreover, you are trying to cram elements into it as into an array. But even if you initialize the sheet and add elements through the Add method, it will not work anyway. Because getting a random index is before adding these very elements, which means there will be from 0 to 0 ....
PS Try

void ChangeColors()    {
        targetCountText.color =  (Color)new Color32(Random.Range(0,256), Random.Range(0,256) , Random.Range(0,256),255);
    }

Unless, of course, a random color is needed not from a specific set ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question