Search Unity

TextAsset.text is "" although the file does have content

Discussion in 'Scripting' started by FlashMuller, Oct 26, 2017.

  1. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Pretty much what the title says:
    I have a TextAsset (*.txt) assigned in the inspector with text in it (!). The TextAsset.text shows me "", although there defenitely is text. No error is thrown. No changes have been made. It has worked already.
    Tried reassigning the file, Refreshing and Reimporting, no change.
    Any ideas? Thanks!
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Thinking about it: I updated Unity....
     
    shimonp21 likes this.
  3. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Weird behaviour: The bytes of the file (TextAsset.bytes) is avaiable. So if you are having the problem, here is a workaround:
    Code (CSharp):
    1. string content = lang.text;
    2. if(content == "")
    3.         content = System.Text.Encoding.Default.GetString(lang.bytes);
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You can also try lang.ToString() to try and get the text, but the .text should also return the text.
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Does a different text file work?

    Maybe that text file has a null character in it or something.
     
  6. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    It's a known bug (reported).
     
  7. St0rmbringer

    St0rmbringer

    Joined:
    Feb 19, 2018
    Posts:
    1
    Still bugged. The workaround posted here works.
     
  8. keni4

    keni4

    Joined:
    Nov 30, 2014
    Posts:
    31
    Got this wired issue on iPhone only. Windows editor and android build was fine. Broke my head, but find out what was a source of problem (besides updating Unity). So, encoding of my json text file was ANSI and TextAsset.text returns empty string with zero length. Setting encoding of the file to UTF-8 do the job.
     
  9. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
  10. MikD

    MikD

    Joined:
    Oct 28, 2018
    Posts:
    3
    This absolutely worked! Re-saved it as UTF-8 and it immediately showed up in the inspector and subsequently in the game. I have no idea how you figured this one out but I am extremely grateful that you did. What a pain! Clearly a patch needs to be instituted for this problem.
     
    porandojin1 and mupstudios_dev like this.