- Home /
Loading TextAsset returns null
Hi,
I'm in the process of writing a simple game and I'm loading some scene description from a text file. I imported my txt file into the assets (it is shown among models, textures and I can see contents in the editor) and load it using this code:
TextAsset textAsset = (TextAsset)Resources.Load("Resources/scene-desc", typeof(TextAsset));
The trouble is that textAsset is always null. I tried placing the fils in the Assets, Assets/Resources folder etc Nothing helped much. What am I doing wrong and how can I overcome this issue? Thanks!
Answer by Bunny83 · Nov 22, 2011 at 04:02 PM
The path you have to provide to Resources.Load is always relative to a Resources folder. Don't include the "Resources" in your path unless you have a Resources folder in your Resources folder (which doesn't make much sense and could even screw up Unity).
Bunny83, I've actually added "Resources" path after failing to load my file anyway. In the end I've created "Resources" fodler inside the "Assets" fodler. Whenever I'm adding new text files there, they are reflected in the "Resources" foder of the Unity assets. $$anonymous$$y main concern is that it might fail in the webplayer or iPhone build. I'm not quite sure these files will be automatically included in the package.
Please use comments unless you want to post an answer to your own question.
All files in any Resources are automatically included in your build. That's the reason why you should be careful what you put in there. All assets in Resources folders are loaded even before the first scene is loaded.
Here's an example what i ment:
"ProjectFolder/Assets/Resources/$$anonymous$$yFile1.txt"
--> required name for Resources.Load is just "$$anonymous$$yFile1" because it's directly in an Resources folder.
You can have as many Resources folders as you like. All count up as one.
Example:
"/Assets/Resources/Subfolder/$$anonymous$$yFile2.txt"
--> "Subfolder/$$anonymous$$yFile2.txt"
"/Assets/SomeOtherFolder/Resources/Subfolder/$$anonymous$$yFile3.txt"
--> "Subfolder/$$anonymous$$yFile3.txt"
thanks, I got it and I read this in the docs. $$anonymous$$y main question regarding wbeplayer/iphone/andriod still remain: would these file be included in the package so that I can still load them using Resources.Load? Thanks.
,Thanks Bunny83, actually I added this Resources path after failing to load it using "scene-desc" on its own. I can only load it when it's in "Assets/Resources/" directory on my drive (whenever I add anything there now it's automatically added to "Resources" folder in Unity3d). I am concerned that I may have troubles with this in webplayer or iPhone build. I am not quite sure whether any file in the resources file are put into final Unity package along with the build.
As i said: All files are included in any build (no matter if you build for web, iPhone, standalone). Since you can load the assets by their file name, Unity doesn't know what assets you will need, that's why Unity includes ALL of them. All files outside a Resources folder are normal assets which have to be referenced by another object / script / scene which is included in the build.
The normal dependency chain is:
- You tell Unity what scenes you want to include in your build. - The scene contains objects which references other assets(prefabs, materials, meshes, sounds, whatever). - All assets that are included can have even more references other to assets (like a prefab that holds a reference to another prefab)
In some cases using the Resources folder might be easier, but you have to keep in $$anonymous$$d that Unity will ALWAYS include everything in your Resources folders, even things you might not even use in your game / application
Your answer
Follow this Question
Related Questions
Cannot edit text file. 0 Answers
How does a TextAsset work? 1 Answer
[Android] Open .txt file from c# 1 Answer
Null Reference Exception while trying to access Text Files 1 Answer
Basic Question about TextAssets 0 Answers