- Home /
Resources.Load / LoadAll
Hi! Load doesn't work like LoadAll?
GameObject loadedAsset = Resources.Load<GameObject>("Prefabs/Ships/" + "shipname");
GameObject[] loadedAssets = Resources.LoadAll<GameObject>("Prefabs/Ships/");
If i print the results LoadAll works but Load returns null. I'm sure i have the path and file name correctly and i have tried adding .prefab after the ship name. Anyone know if there is a way to make it work? Full path is: E:/Game/Assets/Resources/Prefabs/Ships/
The code provided does not compile since Resources.LoadAll
has a return type of GameObject[].
Sorry that was just a mistake. Corrected the original post.
Try to load your Prefab with this method:
public static T Load<T>(string path) where T : Object {
T thing = (T) Resources.Load(path);
if (thing == null)
Debug.LogError("Couldn't load resource '"+typeof(T)+"' with path '"+path+"'");
return thing;
}
Pasted your code and used this to call the method:
GameObject loadedAsset = Load<GameObject>(Application.dataPath + "/Resources/Prefabs/Ships/" + sf.GetSelected() + ".prefab");
Gives me this error: Couldn't load resource 'UnityEngine.GameObject' with path E:/Game/Assets/Resources/Prefabs/Ships/ship1.prefab
Just got a similar setup to work on my computer.
What I did:
var obj = Resources.Load("Prefabs/Ships/shipname");
Debug.Log(obj);
Can you try this code?
Note: I didn't need the ".prefab" to load my prefab.
Note 2: $$anonymous$$ake sure your folder is called "Resources" and not "Resource"
Answer by Imploding_Dragons · May 17, 2015 at 03:20 PM
Just made a prefab with some components and change some data and put it into the Resources folder.
and then loaded like this;
GameObject testobject = Resources.Load("Test") as GameObject;
GameObject.Instantiate(testobject);
Worked fine no problems created the asset into the scene with all the data + components.
Final Notes:
Make sure you have the Case and spelling correct. eg Apple and AppLe is different.
Ok so the problem was that i used a subfolder of a Resources folder. Apparently the file needs to be specifically in Resources. Thank you all for the help!
That Shouldn't be a problem, I made a folder test and a prefab test inside it.
GameObject TempObject = Resources.Load("Test/Test") as GameObject;
and it loaded fine, I just tested it case doesn't seem to matter. Perhaps that particular prefab was glitched and moving it fixed it.
I replaced the prefab now it works even in subfolder. Guess it was a glitch then. Thanks!
Answer by Baste · May 16, 2015 at 09:18 PM
You need to include the extension of the things you are loading. If it's a prefab, the extension is ".prefab", so you'd have to do:
GameObject loadedAsset = Resources.Load<GameObject>("Prefabs/Ships/" + shipname + ".prefab");
@Baste
Resources.Load requires you to omit the extension. So you when you add an extension it certainly won't work. His original code should work as long as the name of his prefab is correct.