Hello.
I have a string variable that receives a text and I would need to change the color of that string. Doing a quick search (using the tag directly in the string), it works, but when showing, the tag is displayed (and it is a behavior that I didn’t want). Is there another way to implement? Or will I have to use a Text or TextMeshPRO? The core idea is to have one part of the string in white and the other part of the string in green. Grateful for the attention.
What are you using now? Just Legacy text?
The text that I want to change the color, is in a string variable:
(I’m finding it impossible to do it this way).
If you have to choose between Text and TextMeshPro, choose the second. TextMeshPro is built-in by default (out of the box) on new versions of Unity.
As for your problem, make sure you follow the pattern below (on the example of the Text object).
string inputText = "Lorem Ipsum";
GetComponent<Text>().text = "<color=green>" + inputText + "</color>";
1 Like
Thanks. So now I can solve my problem.
I don’t know how you handle the rest of the code, but it would be good if the name of the item in the string was unchanged (clean). And if you have some text to be displayed, it should have some scheme (and in it colors, bold, etc.).
string inputText = "Lorem Ipsum";
GetComponent<Text>().text = $"This item is called: <color=green>{inputText}</color>. And it belongs to me.";
1 Like