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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to change the color of text in a .ToString() function?

Discussion in 'Scripting' started by VileGoo, Jun 1, 2018.

  1. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Hi!

    As the title says, I need to change how text looks the way you could with normal strings like
    "Hello <b>person</b>"
    but inside of a .ToString() function. Is this even possible?

    For a quick example, one of my lines of code is
    "\nShiny Powder: x" + int_ShinyPowder.ToString();
    and I need the int_ShinyPowder.ToString() to be red instead of the white that's on the text game object.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, not exactly "inside" but you can put the needed (markup?) before & after.
    Code (csharp):
    1. "\nShiny Powder: x<color=red>" + int_ShinyPowder.ToString() + "</color>";
    Sorry if that colour code isn't correct, I didn't look it up to confirm, but that's the idea. ;)
     
    Brother_77 likes this.
  3. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    In addition, you can do this if you want to use a specific color.
    Code (CSharp):
    1. string fu = "Hello <#CAFFD8>World</color>";
     
  4. VileGoo

    VileGoo

    Joined:
    Apr 29, 2017
    Posts:
    220
    Hi! This is exactly what I needed! Thanks for the help! :)
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. :) You're welcome.
     
  6. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    226
    Thank you so much methos5k!
    I got one more question regarding a string object instead of .ToString method that takes the string as "" parameter. I can't figure out how to do it on the someString example field below

    Code (CSharp):
    1.  
    2. TextMeshProUGUI textRef;
    3. void someMethod(string someString)
    4. {
    5.    int someInt;
    6.    textRef.text = someString + someInt.ToString("<color=red>00</color>");
    7. }

    Edit: I had to do the same color tagging but where the someMethod gets called on the string that is passed as parameter to it.
     
    Last edited: Jan 18, 2023
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780

    Try
    $"<color=red>{someInt}</color>";
     
    Brother_77 likes this.
  8. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    226
    int.ToString("") works with methos5k approach, the problem was someString parameter in the method, but it works when the same approach is performed when the method is called.
     
  9. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    You could always make a wrapper function to do just that:


    Code (CSharp):
    1. string ColoredInt(int num){
    2. return<color=red>+num+</color>;
    3. }
    4.  
    5. “Shiney powder:+ColoredInt(int_shineypowder);
    you could also add a second parameter to add the color, ex “red”.

    note: you dont actually need to write .ToString if you are adding a number to a string with +, that conversion is automatically done for you.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    SF_FrankvHoof and Brother_77 like this.