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.
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
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.
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!!!
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)