Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Changing color of TextMeshPro in code

Discussion in 'UGUI & TextMesh Pro' started by DoomGuy77, Aug 11, 2020.

  1. DoomGuy77

    DoomGuy77

    Joined:
    Aug 25, 2018
    Posts:
    1
    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<TextMeshPro>().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.
     
    Last edited: Aug 11, 2020
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    See the following post

     
    Yept23 likes this.
  3. Josewdf

    Josewdf

    Joined:
    Nov 21, 2019
    Posts:
    15
    I have not the same problem but i can not change the color of my TextMeshProUGUI
    Code (CSharp):
    1. private void UpdateAdStatus()
    2.     {
    3.         stringBuilder.Clear();
    4.  
    5.         if (GameStats.Instance.GetRemoveAds())
    6.         {
    7.             stringBuilder.Append("OFF");
    8.             txtAdsStatus.color = new Color(15, 98, 230, 255);
    9.         }
    10.         else
    11.         {
    12.             stringBuilder.Append("ON");
    13.             txtAdsStatus.color = new Color(222, 41, 22, 255);
    14.         }
    15.  
    16.         txtAdsStatus.text = stringBuilder.ToString();
    17.     }
    i have this code and it keeps showing the color of the text in white.

    i hope somebody could help me. thanks
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Color uses floats and not bytes which Color32 does.
     
  5. Josewdf

    Josewdf

    Joined:
    Nov 21, 2019
    Posts:
    15
    could you give me a little example of how to do it??
     
  6. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Code (csharp):
    1.  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.
     
  7. Josewdf

    Josewdf

    Joined:
    Nov 21, 2019
    Posts:
    15
    thanks for your answer. I changed thee class Color by Color32 and is now perfect.
    Code (CSharp):
    1. private void UpdateAdStatus()
    2.     {
    3.         stringBuilder.Clear();
    4.  
    5.         if (GameStats.Instance.GetRemoveAds())
    6.         {
    7.             stringBuilder.Append("OFF");
    8.             txtAdsStatus.color = new Color32(15, 98, 230, 255);
    9.         }
    10.         else
    11.         {
    12.             stringBuilder.Append("ON");
    13.             txtAdsStatus.color = new Color32(222, 41, 22, 255);
    14.         }
    15.  
    16.         txtAdsStatus.text = stringBuilder.ToString();
    17.     }
     
  8. aaronrums

    aaronrums

    Joined:
    Jan 9, 2021
    Posts:
    3
    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.
    Code (CSharp):
    1. using TMPro;
    2. // other using statements....
    3.  
    4. private TextMeshProUGUI escapeText;  
    5.  
    6.     void Start()
    7.     {
    8. //need to use GameObject.Find first because it's not on the same game object as this script.
    9.         escapeText = GameObject.Find("EscapeText").GetComponent <TextMeshProUGUI>();
    10.     }
    11.  
    12. void Update()
    13. {
    14. escapeText.color = new Color (1,1,1, 0.5f);
    15. }
    16. //change the transparency, as an example
     
  9. Josewdf

    Josewdf

    Joined:
    Nov 21, 2019
    Posts:
    15
    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
     
  10. mamicelewilson

    mamicelewilson

    Joined:
    Jan 22, 2020
    Posts:
    1
    use "faceColor" valor , i think that works cause , this , is changing the material of TMpro


    Code (CSharp):
    1. [COLOR=#0000ff]using [/COLOR][COLOR=#000000]UnityEngine;[/COLOR]
    2. [COLOR=#0000ff]using [/COLOR][COLOR=#404040]TMPro;[/COLOR]
    3.  
    4. [COLOR=#0000ff]public class[/COLOR][COLOR=#00b359] elemento : MonoBehaviour[/COLOR]
    5. {
    6.   [COLOR=#0000ff]  public[/COLOR][COLOR=#00ff00] [/COLOR][COLOR=#00b300]TMP_Text[/COLOR][COLOR=#404040] Numero;[/COLOR]
    7.  
    8.     [COLOR=#0000ff]private void Awake[/COLOR]()
    9.     {
    10.         Numero = [COLOR=#b3b300]GetComponent[/COLOR]<[COLOR=#00b300]TMP_Text[/COLOR]>();
    11.         Numero.faceColor = [COLOR=#00b359]Color[/COLOR].black;
    12.     }
    13.  
    14.  
    15. }
     
  11. StealthMode_JW

    StealthMode_JW

    Joined:
    Jul 10, 2015
    Posts:
    3
    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. TextMeshPro Color Text issues.png
     
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    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
     
  13. brombs73

    brombs73

    Joined:
    Jul 21, 2012
    Posts:
    10
    Hi i had the same. Try setting the VertexColor to white, then the colors being set on the material are visible (for example faceColor).
     
    renman3000 likes this.
  14. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    yes. I think if the core color starts at white, it should be ok.
     
  15. abitowhit

    abitowhit

    Joined:
    Aug 9, 2020
    Posts:
    13
    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)