Assign Text Colour Value

Hi

I am using Random. Range on few pieces of text, is there a something I can use to assign each word with a hidden colour value.

The text itself changes through a random colours but I would like to give word below a specific colour value which I can then use to relate it to a sprite.

    public Text colourText;

    public string[] names = new string[] { "Red", "Yellow", "Blue", "Green" };

    public string GetRandomName ()

    {
        return names[Random.Range(0, names.Length)];
    }

    void Start()
    {
        colourText.text = (GetRandomName());
        colourText.color = new Color(Random.Range(0f, 1f),Random.Range(0f, 1f),Random.Range(0f, 1f),1);
    }
}

You could create a class for your color information. Try this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ColorInfo
{
    public Color color;
    public string colorName;
}

public class Test : MonoBehaviour
{
    public ColorInfo[] colorInfo =
        new ColorInfo[] {
            new ColorInfo {
                color = new Color(1,1,1),
                colorName = "White"
            },
            new ColorInfo {
                color = new Color(.5f,.5f,.5f),
                colorName = "Gray"
            }
        };

    public Text colourText;

    void Start()
    {
        ColorInfo colorInfo = GetRandomColorInfo();

        colourText.text = colorInfo.colorName;
        colourText.color = colorInfo.color;
    }

    public ColorInfo GetRandomColorInfo()
    {
        return colorInfo[Random.Range(0, colorInfo.Length)];
    }
}
1 Like