Search Unity

How to change colour of text in a text box?

Discussion in 'Scripting' started by Tovey-Ansell, Aug 1, 2016.

  1. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    I know there have been a few threads on this already, but I just can't make sense of the answers.

    I'm making a command line interface game, and I want to have errors, like "command not recognised" appear in the output text box as red, while keeping the other text the default colour, here's what I have:

    switch (inputCommand)
    {

    case "MainEngineStart;":
    Debug.Log("ROCKET HAS BEEN LAUNCHED");
    startTime = Time.time;
    break;

    default:
    textOutput.text = (textOutput.text + "\t ERROR: Command not recognised") + "\n";
    break;
    }

    can I really not just have something like "\t ERROR: Command not recognised".ColourRed() ?

    This is my first post, any help would be appreciated :)

    Thanks,
    Fred T.A
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    pretty sure you can do something like

    Code (CSharp):
    1. textOutput.color = new Color(1,0,0,1); // the r g b a values u want.
    2. textOutput.text = (textOutput.text + "\t ERROR: Command not recognised") + "\n";
    3. break;
    it will set new rgb values before being called,
     
    Tovey-Ansell likes this.
  3. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    Awesome thanks that now changed the colour :D

    This presents a new problem though, to add next to the textOutput text box I'm setting the text to what's already in the text box, plus the new text that I want to output, this means that I'm effectively re-writing the whole text each time, is there anyway around this?
     
  4. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Uhh, I can't quite make out what you mean..

    Do you already have something in the text box and simply want to add "\t ERROR: Command not recognised" to the end of it?
     
  5. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    Yeah, exactly, so I keep the old lines of text in the box, and add a new line underneath it
     
  6. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Will there be more text to be added to the same textfield in inspector? or just them two lines, if not you can do.

    Code (CSharp):
    1. private string currenttext; // the current text in the textbox,
    2.  
    3. void Start()
    4. {
    5.   currenttext = textOutput.text; // store in here
    6. }
    7.  
    8. // then when you want to add it to the new one
    9.  
    10. textOutput.color = new Color(1,0,0,1);
    11. textOutput.text = oldtext + "\n" + ERROR: Command not recognised" + "\n";
    This should display what you want, but will change all the text to red, if you don't want this I can further assist :p
     
  7. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    I've attached a picture of what I mean, so yeah I will need more text in the field, what I'm aiming for is to use the colours black for default text, and green, red and yellow for other text.

    In the image, the error messages would be in red
     

    Attached Files:

  8. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Ah okay so a good way for this is specifying all the text you want to output in an array of strings in script.

    Code (CSharp):
    1. public string[] textstuff = new string[6] /
    2.  
    3. void Start()
    4. {
    5.   // using the hash codes for different values (you can get these from the RGB picker in inspector red = E10707D0
    6. textstuff[0] = <color=#E10707D0> stuff typed in red</color>
    7. textstuff[1] = <color=#E10707D0> stuff typed in orange</color>
    8. StartCoroutine(swaptext());
    9. }
    10.  
    11. then run this all in a coroutine
    12.  
    13. IEnumerator swaptext()
    14. {
    15.   bool start = false;
    16. string temptext = " ";
    17.   for (int i = 0; i < textstuff.length; i++)
    18. {
    19. while (!start)
    20. {
    21. {
    22.     temptext+= string.Format("{0}\n", textstuff[i]);
    23. yield return null;
    24. }
    25. textOutput.text = tempString;
    26. }
    27. }
    28. }
    29. }
    try this out, see if it displays all the different colors you want in the textfield (without input) then if so, you can start adding input so (i) increments by 1 to display new lines.
     
  9. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    Thanks for the help, I'll give it a go :)

    I also just found out that changing the colour of the text changes the colour of all text in the box, not just text that is output after the colour is changed..

    so if I had
    output "1"
    output "2"
    change to red
    output "3"

    we wouldn't get black numbers 1 and 2 and a red 3, we would get black 1 and 2, which would then turn red, with a red 3 added on

    ..that's a problem right?
     
  10. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Hmm, try my method, if it's a problem ill try figure a way around this! :p
     
  11. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    Given this a good go but can't make it compile, the problem seems to be with the line

    textstuff[0]=<color=#E10707D0>

    It doesn't want to assign a colour to a string, throwing "color does not exist in the current context" errors, etc
     
  12. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    If you run this
    Code (CSharp):
    1. public class TextScript : MonoBehaviour {
    2.  
    3.     public string[] textstuff = new string[6];
    4.  
    5.     public Text textthing;
    6.     string tempstring = " ";
    7.     void Start()
    8.     {
    9.         textstuff[0] = "<color=#E10707D0> stuff typed in red</color>";
    10.         textstuff[1] = "<color=#FF911CFF> stuff typed in orange</color>";
    11.         textstuff[2] = "<color=#E10707D0> stuff typed in red</color>";
    12.         textstuff[3] = "<color=#FF911CFF> stuff typed in orange</color>";
    13.         textstuff[4] = "<color=#E10707D0> stuff typed in red</color>";
    14.         textstuff[5] = "<color=#FF911CFF> stuff typed in orange</color>";
    15.         for (int i = 0; i < textstuff.Length; i++)
    16.         {
    17.            tempstring += string.Format("{0}\n", textstuff[i]);
    18.  
    19.         }
    20.         textthing.text = tempstring;
    21.     }
    22.  
    23.  
    24.    
    25. }
    It will provide different colors, did you remember to do speechmarks for the strings?

    The next step is to do an iteration where i increases on certain events to add the next line, what i provided just does it all on start :p
     
    Tovey-Ansell likes this.
  13. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    150
    Ah brilliant that works perfectly! Thanks a lot, been banging my head against this for ages (first Unity project)

    This lets me output certain lines different colours, while not changing the colour of the other text, perfect

    Thanks again!
     
  14. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Welcome! goodluck with your first project :)