Changing color of TextMeshPro in code

Hi there.
I’ve got a TextMeshPro gameobject that I’m trying to change the color of in the code (hence the title). I’m refering to it using ‘this.gameObject.GetComponent().color’ (the script is attatched to the text) but I’m getting the following error:

error CS0246: The type or namespace name ‘TextMeshPro’ could not be found (are you missing a using directive or an assembly reference?)

From the error says, I would assume the problem is in the GetComponent part, but after looking at other posts on this forum and around the internet, it looks like it should work. Other posts (and the FAQ) have mentioned that it is different for UI text but I am using the 3D GameObject TextMeshPro text.

Any help would be appreciated.

See the following post

1 Like

I have not the same problem but i can not change the color of my TextMeshProUGUI

private void UpdateAdStatus()
    {
        stringBuilder.Clear();

        if (GameStats.Instance.GetRemoveAds())
        {
            stringBuilder.Append("OFF");
            txtAdsStatus.color = new Color(15, 98, 230, 255);
        }
        else
        {
            stringBuilder.Append("ON");
            txtAdsStatus.color = new Color(222, 41, 22, 255);
        }

        txtAdsStatus.text = stringBuilder.ToString();
    }

i have this code and it keeps showing the color of the text in white.

i hope somebody could help me. thanks

1 Like

Color uses floats and not bytes which Color32 does.

1 Like

could you give me a little example of how to do it??

 Color c = new Color(1.0f, 0.8f, 0f, 1.0f);

You are using bytes (0 - 255) values instead of floats of (0 - 1) range.

4 Likes

thanks for your answer. I changed thee class Color by Color32 and is now perfect.

private void UpdateAdStatus()
    {
        stringBuilder.Clear();

        if (GameStats.Instance.GetRemoveAds())
        {
            stringBuilder.Append("OFF");
            txtAdsStatus.color = new Color32(15, 98, 230, 255);
        }
        else
        {
            stringBuilder.Append("ON");
            txtAdsStatus.color = new Color32(222, 41, 22, 255);
        }

        txtAdsStatus.text = stringBuilder.ToString();
    }
7 Likes

yeah, as the post from Stephen_B alludes to, I think you need to change the component that you’re looking for. Here’s some code that I was just working on.

using TMPro;
// other using statements....

private TextMeshProUGUI escapeText;   

    void Start()
    {
//need to use GameObject.Find first because it's not on the same game object as this script.
        escapeText = GameObject.Find("EscapeText").GetComponent <TextMeshProUGUI>();
    }

void Update()
{
escapeText.color = new Color (1,1,1, 0.5f);
}
//change the transparency, as an example

I used Color32 because i wanted to just copy the values from the Unity Editor into my code and it worked perfectly with the code i showed before. Thanks

use “faceColor” valor , i think that works cause , this , is changing the material of TMpro

[COLOR=#0000ff]using [/COLOR][COLOR=#000000]UnityEngine;[/COLOR]
[COLOR=#0000ff]using [/COLOR][COLOR=#404040]TMPro;[/COLOR]

[COLOR=#0000ff]public class[/COLOR][COLOR=#00b359] elemento : MonoBehaviour[/COLOR]
{
  [COLOR=#0000ff]  public[/COLOR][COLOR=#00ff00] [/COLOR][COLOR=#00b300]TMP_Text[/COLOR][COLOR=#404040] Numero;[/COLOR]

    [COLOR=#0000ff]private void Awake[/COLOR]()
    {
        Numero = [COLOR=#b3b300]GetComponent[/COLOR]<[COLOR=#00b300]TMP_Text[/COLOR]>();
        Numero.faceColor = [COLOR=#00b359]Color[/COLOR].black;
    }


}

FIXED IT!
For anyone else having this issue, none of the above examples worked for my case.

In my case, I was trying to change the Text color based on my own variables (RED for invalid, BLUE for valid). I found that whenever I changed a Material or Shader or any significant piece of the component, it would reset my pre-chosen variable colors to black. So then I would change them back to RED and BLUE. Each time I ran it, I had every indication the component was being grabbed properly and that the text was being populated in the Component field, but just not showing up in game. And it took me awhile to realize that when my component resets it’s values and thus changes my preset colors to black, it was also changing the opacity of each to 0. So even after I reset my public references to RED and BLUE, and I could clearly see them in the Inspector, they weren’t showing up in game, simply because the opacity was reset to 0, too.

I’m wondering if this was happening to anyone else. Because now, I can try all the methods above and they all work just fine.

6 Likes

Hi @Stephan_B
I am experiencing glitchiness when trying to change the color of the text.

It seems to apply to all, color, facecolor and outline color… for example when I set any to = Color.white, it does not do anything. However, if I set them to = Color.black, this does work!!!

Any ideas?

Thank you

Hi i had the same. Try setting the VertexColor to white, then the colors being set on the material are visible (for example faceColor).

1 Like

yes. I think if the core color starts at white, it should be ok.

Seriously??? Well there you are! lol
Thank you so much for this response, it was my exact same issue and fixed mine as well.
I knew it was being set, problem was I just couldn’t see them.
At first glance, I was hoping I could just force opacity in the code (probably could in the color) but setting it in designer was simple enough (once I actually comprehended what you were saying - totally overlooking your big red arrow - heh)

Realy help full.