TextAsset always returns null despite the file existing?

I’m trying to have Unity read from text files, and oddly enough it returns a null TextAsset for me every time. I have tried several things to get it to work, and searching here on Answers and Google yielded nothing, and checked the documentation. I also checked here and while there were some questions related to this, the answers are oddly not working.

Now I hope I read it right, but TextAsset can’t be used for writing text files; only reading.

dbfile = "/GameData/items.txt"

TextAsset t = Resources.Load (dbfile) as TextAsset;

if (t == null)
		Debug.Log ("No Textasset! D:");

Unfortunately, I always got null. It seems to be finding the file (otherwise I’d get a FileNotFound exception) I have tried several things to get this working (based on the answers I found):

-Removed the “.txt” extension.

-Renamed GameData to Resources according to one post.

-Removed the first “/” character.

-Throw items.text to the “Assets” folder.

-Tried another method of casting TextAsset (via TextAsset t = (TextAsset)Resources.Load(“/GameData/items.txt”,typeof(TextAsset)) )

All, if they didn’t give me a file not found exception, only returned null. (Reading the file using the File.ReadAllLines and etc works just fine, so the file is there). Am I doing something else wrong with TextAsset/am wrong to assume the TextAsset class can be used to get data from a text file bundled as an asset?

The only valid path would be

dbfile = "GameData/items";

However make sure you placed your items file here:


“Resources.Load” can only load resources which are in a “resources” folder. The path is always relative to the resources folder.

The usual prefered way would be to just declare a public variable like that:

public TextAsset itemsDB;

and assign your text asset file in the editor to the variable. This has several advantages:

  • The file doesn’t need to be in a resources folder. It can be anywhere inside the assets folder.
  • Since you assigned the reference in the editor, Unity doesn’t have to search for the asset in all resources folders. The reference is serialized.
  • Unity automatically detects all assets which are referenced by some serialized variable and only includes those assets into a build. Things inside Resources folder are always included, no matter if you use it or not since you can load them dynamically via code.

I am encountering the same issue. Any update on this?

I had a very similar problem and since I can’t find much help on fixing it, I’ll post it my solution here.

I was creating bytes files then trying to read them as text assets afterwards and they were coming through as null even though the file existed. The problem turns out to be that it was missing a meta file that contained text import information which wasn’t being built for it during runtime.

Calling AssetDatabase.Refresh forced Unity to generate the meta files and fixed the problem however I havn’t tested it from a build so its not a perfect solution.

I hope this sheds some light on anyone stuck with the same kind of problem. Credit to this answer for helping me.

For me this was solved…

Basically I was reading a file called MG_Data.data that contained my entire games xml database. The problem is that if something is named ‘.data’ it’s not gonna load. It MUST have the ‘.txt’ extension to be loaded using Resources.Load(pathNameGoesHere);