Search Unity

Unity replacing > character from string

Discussion in 'Scripting' started by BadSeedProductions, Jan 18, 2020.

  1. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    I'm trying to use rich text, following the documentation exactly but it seems there is a bug (using 2018.3.0f2) where it replaces the greater than character from my markup tags with a less than character. For example if I type:
    Code (CSharp):
    1. string boldTxt = "<b>I want this text bold</b>";
    then unity prints
    Code (CSharp):
    1. <b<I want this text bold</b<
    Is there a workaround for this?
    edit: Rich text works if I type the tags into the the inspector but I'm trying to use a script
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I haven't encountered this issue. Are you putting it into a textmeshpro text or a regular Unity text?

    You could put an @ in front of your text which may solve the issue, but > is not a special escape char, so it shouldn't make a difference.


    Code (CSharp):
    1. string text = @"<b>I am text</b>";
     
  3. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    I'm using regular Unity text. Tried the @ in front, didn't make a difference.

    edit:
    I've tried delegating the string, I've tried replacing the '>' with &gt; and then .Replace("&gt;", ">") and still no luck.
     
    Last edited: Jan 18, 2020
  4. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Could you post the whole code? Maybe the string gets modified somewhere else. I am using 2018.1.3f1 and it works fine if I am doing it like this:

    Code (CSharp):
    1. string txt = "<b>hello</b>";
    2.     // Use this for initialization
    3.     void Start () {
    4.         GetComponent<Text>().text = txt;
    5.     }
     
  5. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    SOLVED (somewhat)

    Thanks for testing with2018.1.3,

    So I made a fresh script and UI text element just to make sure there was nothing else effecting it and low and behold the code I posted is working from that script something was effecting it. The <color> tag.
    An appended color tag with spaces. <color = green> should be <color=green>. Taking out the spaces fixed it. But here's where it gets weirder. I can't recreate the flipping around of the greater than sign on the new script. If I leave spaces it simply prints out the <b> as is. So I never did figure out what's flipping the greater than character into a less than. So weird.... But I'm accepting this as a mystery and moving on since taking out the spaces from the color tag worked.
     
  6. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Glad you could solve it, though those things are a nightmare because you never know when it it come again to haunt you
    I would suggest you try out TextMeshPro since it is part of Unity and gives much clearer text and a ton of options. For example I used it to make links in the text that show info boxes when hovering over it.
     
    BadSeedProductions likes this.