- Home /
Resources.Load wont read text file
Okay, So I spent the better part of yesterday trying to figure out why it wasn't working and got absolutely nowhere. My code is simply as follows:
TextAsset SourceFile = (TextAsset)Resources.Load("DataFile", typeof(TextAsset));
The main data file I am attempting to load, and have been successfully able to load in since day one until I upgraded to Unity 2017, is a Tab Delimited Export from Excel. Up to this point I have never had any trouble at all loading the file. But for some reason now, when the code loads the source file, the result is simply "", with no actual string or anything found.
Just to confirm that it was NOT my code, I created a simple text file with some garbage text and it loaded in just fine. The inspector preview in Unity shows the garbage text no problem. But the source file I've been attempting to load does not show anything in the preview, and also doesn't load anything when I call it with Resources.Load.
I am absolutely baffled as to why it's not working now. I've attempted reimporting the asset. recreating the asset entirely. You name it, I can't figure out why it's not loading it in. The file isn't massive either. it's less than 80kb.
Any help would be greatly appreciated because at this point It's preventing me from going further with development.
Answer by Lempereur · Oct 24, 2017 at 07:56 PM
So be it by decision of the gods of programming... Apparently loading a similar formatted file from a different project... Loaded in just fine.. So i took the contents of the original source and copied it over to the newly imported file... and Lo and behold! it worked....
The only thing I can think of is that somehow during the process of upgrading the project file to Unity 2017, it mushed with the old TD file and messed it up somehow. I guess the hash value was similar enough on the file that it never made any changes. So it just stayed as being broken. But all is well now, however this may be something the devs wish to address because I guarantee I'm not the only person loading data in from either a CSV or a Tab Delimited text file that has run into this issue.
Have you tried diffing this new working version of the file with the one that wasn't working? (with the diff tool set to show all differences, of course). Without knowing exactly what was wrong with it, I'd be loath to draw conclusions about what's happened here.
Used Win$$anonymous$$erge to do a diff comparison between the two files like you suggested. The only difference between the two was at the very end of the file where there was an extra blank line. There were literally no other differences.
Now I noticed that in the original file before, and deleted the extra line and saved the file and tried again to no avail. But I really don't see an extra blank line as the reason to cause a text asset parser to barf.
The logic I have to actually parse the string for all the data -after- it has been read by Resources.Load (as was previously intended) is sophisticated enough to not barf at a blank line. That being said, the code isn't even getting to that point because the Resources.Load method would return an empty string without any error messages.
How very strange! Don't suppose it could have been a file permissions issue..?
Answer by hi_mark_pope · Mar 28, 2018 at 09:33 PM
I had an issue in which the editor would not preview a text file, and loading it at runtime with Resources.Load(filename) would result in an empty string.
I tried chopping the file down until it would work, and gradually narrowed the issue down.
The file contained a slanted apostrophe (ascii value of 0x92 = 146 decimal). Changing this to a regular vertical apostrophe (ascii 0x27) make the file load correctly!
Regular ascii values go from 0..127, so I think the TextAsset loader judges the file to be invalid as a text file and doesn't load it.
This is the answer! ! ! ! O$$anonymous$$G... I deleted all the apostrophes in my text file and the .text file is no longer blank in Unity.
You're awesome! Unity really needs to fix this .. You should tell them the solution. :)
Answer by Bodhid · Oct 24, 2017 at 03:14 PM
I tried it out, i placed a .txt file in a folder called "Resources". It works just fine, to get the text in a string:
TextAsset SourceFile = (TextAsset)Resources.Load("DataFile", typeof(TextAsset));
string text = SourceFile.text;
That's not the problem though. Putting a simple text file in the resources I can load it just fine. The problem is the tab delimited export from Excel is being read in as nothing.
During debug, when the text file is read in... the result is "". meaning literally nothing is being read in. making a string variable and setting it to SourceFile.text doesn't make a difference when what's being read in results in "". It reads in an empty string. That's the issue.
A basic .txt file reads in just fine. But the TD file doesn't read in anything at all.
Answer by ghodofredo · Nov 22, 2017 at 05:19 PM
when trying to load a large text, for example a story for an RPG game, textasset does not work
Answer by keni4 · Jun 08, 2018 at 12:53 PM
Had the same issue. Changing text file encoding from ANSI to UTF-8 helps in my case.