- Home /
Subfolders with Resources.Load not working
It seems that using subfolders inside Resources no longer works with Resources.Load
Examples:
Works:
var ind = Instantiate(Resources.Load("Indicator", typeof(GameObject))) as GameObject;
Folder structure:
Resources
— Indicator.prefab
According to this and a few other sources, this should work, yet it doesn't:
var ind = Instantiate(Resources.Load("UI/Indicator", typeof(GameObject))) as GameObject;
Folder structure:
Resources
— UI
— — Indicator.prefab
The documentation says:
The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
If it's relative, then it should be working.
The only other way I've found to be able to organize the assets is to put a "Resources" folder inside every asset category. This really doesn't seem ideal to me, and will most likely result in a mess:
var ind = Instantiate(Resources.Load("Indicator", typeof(GameObject))) as GameObject;
Folder structure:
Resources
— UI
— — Resources
— — — Indicator.prefab
Does Unity really not support subfolders inside Resources anymore? Or am I doing something wrong? I've been trying to wrap my head around something I must be doing wrong, but it's getting quite frustrating and I've found no reason for it not to work.
Additionally, I've tried posting this issue on the Unity Support forum, but everytime I click on Submit New Thread, it does nothing and takes me to the forum.
Answer by advancexz · Nov 11, 2016 at 08:33 AM
just for the record: if you have another resource with the same name (different extension) in the same folder the Resource.Load will always return NULL, just realized after hours of struggle with this. In my particular case:
transition_bar.png
transition_bar.prefab -> transition_bar_pfb.prefab
Answer by TonyLi · Jul 22, 2013 at 05:05 PM
Loading resources from subfolders works fine for me in Unity 3.5 and Unity 4.
What about trying to break down each step?
Object o = Resources.Load("UI/Indicator");
if (o == null) Debug.Log("Load failed");
GameObject go = o as GameObject;
if (go == null) Debug.Log("Loaded object isn't GameObject");
GameObject ind = Instantiate(go) as GameObject;
if (ind == null) Debug.Log("Couldn't instantiate");
I'm guessing that one of the first two Debug.Log lines will report, and you'll be able to narrow down the issue.
Which version of Unity 4 are you using? I just updated to 4.2 and it now works, with the same code. I'm guessing it was a bug in Unity 4.1.5f
Hmm, I'm using 4.1.5f. I didn't know 4.2 was out yet. I guess I'll update today. Anyway, I'm glad it's working now for you.
Your answer
Follow this Question
Related Questions
Loading in Sprites outside of unity? 1 Answer
Resources.Load renaming/moving resources. 0 Answers
Resources.Load problem 1 Answer
Resources.Load wont read text file 6 Answers
Loading Asset in Android using StreamingAssets and UnityWebRequest 1 Answer