Resources.Load can't seem to find the file.

I have a local text file at this location “Assets\DataFiles\DataFile1.txt” and I have tried many different things to get the TextAsset to load.

Using any of these syntax I was never able to get the file to load(null reference).

1)TextAsset dataFile = (TextAsset)Resources.Load(“DataFile1”, typeof(TextAsset));
2)TextAsset dataFile = (TextAsset)Resources.Load(“DataFiles/DataFile1”, typeof(TextAsset));
3)TextAsset dataFile = (TextAsset)Resources.Load(“Assets/DataFiles/DataFile1”, typeof(TextAsset));

I also tried moving the file to the Assets folder and using these:

1)TextAsset dataFile = (TextAsset)Resources.Load(“DataFile1”, typeof(TextAsset));
2)TextAsset dataFile = (TextAsset)Resources.Load(“Assets/DataFile1”, typeof(TextAsset));

Nothing worked for me.

I did get it to work by creating a public variable and attaching the file to it via inspector.
public TextAsset dataFile;

Any ideas why I couldn’t get the “Resources.Load” to find the file?

RTFM?

“All assets that are in a folder named “Resources” anywhere in the Assets folder can be accessed via the Resources.Load functions”

5 Likes

You seem like a nice guy, not.
I did read it…
Anyway yeah I tried the file in the Resources directory.
Same thing.

Do the world a favor and RTFM on how to coexist.

5 Likes

You kidding? Erry day posts here can be solved by reading the docs or searching the forums and answers, and I make helpful posts when I can

The following works fine for me:
TextAsset text = (TextAsset) Resources.Load( “someTextFile”, typeof( TextAsset ) );
Note that you dont need to specify the assets path, because it’s ‘relative to any Resources folder inside the Assets folder of your project’, in this case my file is at
“Assets/Resources/someTextFile.txt”

They can also be discussed on the forum. Sounds like if it’s working for you then maybe something else in the project or environment is affecting it. Try running the script in a new project to rule it out.

Hey,

I just spent the past 30 minutes battling the forces of Resources.Load and left-over carriage-return characters, so I can partially relate to your troubles. (In my case, the issue was due to the fact that splitting a file along new-line characters didn’t remove carriage-return characters present on each line, resulting in blood and violence)

Looking over your initial post, I can only really recommend trying again to place the text file in the Resources/ folder:
Assets/Resources/Text.txt
(note that “Resources” must be at the root of “Assets”) and calling it:
TextAsset asset = Resources.Load (“Text”);
(note that in the path, you should include neither the Resources/ part, nor the file extension)
On a side-note, if you were to place the Text.txt file into subdirectory Sub/ inside Resources/, i.e.
Assets/Resources/Sub/Text.txt
then you would load it with:
TextAsset asset = Resources.Load (“Sub/Text”);

6 Likes

Another common mistake: forgetting to remove the file extension from the file name in c#:
TextAsset gameInfoAsset = Resources.Load(“GameInfo.txt”) as TextAsset; //WRONG
TextAsset gameInfoAsset = Resources.Load(“GameInfo”) as TextAsset; //CORRECT

9 Likes

Guys,

Tried each and every scenario. As @LootlabGames and @avirtualfox mentioned, I could not resolve this problem. Are there any specific settings i need to do for the script to load my xlsx file from Resources folder under Assets?

1 Like

Create a Resources folder in the Assets. Create / move your file to this folder. Then you can call the load method without a path.

For example I have a material in Assets\Resources\black.mat

Material TeleportAreaVisible = (Material)Resources.Load(“black”, typeof(Material));

1 Like

I just struggled through this for an hour, the main problem was in the end my path variable had two slashes in them that I could not see (text too small I guess). I need a fairly deep directory structure to keep track of things, and that is a downside.

However a few things that might help:

  • Make sure the text file’s “meta” files has type “TextScriptImporter:” (look inside). Otherwise it won’t be a TextAsset.
  • I had to edit this to set it, I think DOS line endings “\r\n” might throw it off, and I changed them and it seemed to help, but in the end it read a csv with \r\n endings ok. So maybe not…
  • I changed the DOS line endings with Notepad2 (find and replace \r\n with \n and option interpret backslash set).
  • My file had the “.csv” extension, but I had to do “Resources.Load(resFullName);”` without the extension.
  • Sometimes the resources don’t get compiled and you have stale content. I noticed that sometimes an older file gets read in (when it actually works).
  • I am not sure how to force the resources to be compiled. I would like to know.
1 Like

LOVE YOU!

3 Likes

@LootlabGames I know this is coming up on 7 years old, but there’s multiple suggestions and according to your details your specific issue should be solved by @XSpitFire 's answer.

Your asset path doesn’t contain a “Resources” folder. Create this folder, place your assets in there, and try again.

Perfect.

Also make sure that you are not using UnityEngine.TextCore.Text.TextAsset !
When I was using it, Resources.Load(some file) would always return Null. As soon as I removed the using TextAsset = UnityEngine.TextCore.Text.TextAsset; from the top of my script, it worked.

As an aside, depending on the file’s extension, you may have to make sure that the asset is being imported as a text file (or just change the file’s actual extension to txt.)

1 Like

I’m using JetBrains Rider as an IDE and it used my “Resources” directory as a namespace. Totally screwed up my reference to the real “Resource.Load”.

I needed to right click the “Resources” directory (it says “(Editor)” beside it), right clicked and selected “Properties” to open up the window in this image:

I deselected “Namespace Provider” and then changes my namespaces in the source where needed.

(Further namespace help here: Adjust Namespaces | JetBrains Rider Documentation)

Good luck

1 Like