Home > Blockchain >  How can I set the color of words as they are added to a TMPro UI element?
How can I set the color of words as they are added to a TMPro UI element?

Time:12-13

I've got a script that is added words according to an order when you pick them up, but when you get the order wrong, it is filling in the words that should have gone in that order. Most of the script is listed below, but I'm struggling to find a solution to having the words that are correct (In the onm ouseDown else if statment) being added with the color white, and the other two SetText calls adding the words with the color red.

`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class WordOrderOnDestroy : MonoBehaviour
{
        private TextMeshProUGUI pickedUpWords;

    void Start()
    {
                pickedUpWords = GameObject.Find("Picked Up Words").GetComponent<TextMeshProUGUI>();
    }

    void onm ouseDown() 
    {
        if (gameObject.name != memorySlotOne[currentIndexOne])
        {
            GetComponent<MeshRenderer>().material = redForWrong;
            StartCoroutine(DestroyObjects());
        }
        else if (gameObject.name == memorySlotOne[currentIndexOne])
        {
            Debug.Log("Correct!");
            pickedUpWords.SetText(pickedUpWords.text   memorySlotOne[currentIndexOne]   " ");
            Destroy(gameObject);
        }
    }

    IEnumerator DestroyObjects()
    {
        for (int i = currentIndexOne; i < memorySlotOne.Count; i  )
        {
            if (memorySlotOne[i] == gameObject.name)
            {
                Debug.Log("And then, "   (memorySlotOne[i]));
                Destroy(gameObject);
                memoryListManager.currentIndexOne  ;
                pickedUpWords.SetText(pickedUpWords.text   memorySlotOne[i]   " ");
                break;
            }
            // Append the current word to the text mesh pro UI element
             pickedUpWords.SetText(pickedUpWords.text   memorySlotOne[i]   " ");

            yield return new WaitForSeconds(rateOfDestroy);
            Destroy(GameObject.Find(memorySlotOne[i]));
            Debug.Log("Next correct word was: "   (memorySlotOne[i]));
        }
        
    }
}

`

CodePudding user response:

TextMeshPro supports Rich Text so you can simply wrap your words in e.g.

"<color=red>"   memorySlotOne[currentIndexOne]   "</color>"

Might need to enable TEMP_Text.richText (should be default though afaik)

  • Related