- Home /
LoadAssetAtPath() returning null IN EDITOR
Hello,
I've searched and searched and mostly find people trying to use it for runtime stuff with builds and getting null.
Well, I'm getting null from within the editor. No play mode involved.
I have a static class which is used by a custom EditorWindow class. Everything in my editor works fine as far as saving, editing, etc. Except for loading in a prefab.
I have tried AssetDatabase and Resources. And both the typed and untyped versions of the Resources function. I also tried LoadMainAssetAtPath(). Have tried both with and without the .prefab extension. Have tried the full path, even the path without "Assets". Everything returns null.
I'm actually using the OpenFilePanel to get the path but have hardcoded it to be sure the path is indeed correct and for a better example.
I've tried every suggestion and everything I can think of and am hoping someone can tell me what's up.
The one thing I haven't tried, and I'm sure would work. Is a Resources folder and Resources.Load(). But I don't want or need these prefabs loaded or bundled at run time so LoadAssetAtPath() seems like exactly what I need but I can't make it work.
Unity Consoles 4.3.4f1
GameObject temp1 = Resources.LoadAssetAtPath<GameObject>("Assets/[DragonBuilder]/NewDragon.prefab");
GameObject temp2 = (GameObject)Resources.LoadAssetAtPath("Assets/[DragonBuilder]/NewDragon.prefab", typeof(GameObject));
GameObject temp3 = (GameObject)AssetDatabase.LoadMainAssetAtPath("Assets/[DragonBuilder]/NewDragon.prefab");
Debug.Log(temp1);
Debug.Log(temp2);
Debug.Log(temp3);
http://docs.unity3d.com/ScriptReference/Resources.LoadAssetAtPath.html http://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAssetAtPath.html
Thanks!
All the things you do expect a GameObject to be returned. Have you tried not to cast it and find out whever it was a different type ? An instance of some other subtype of UnityEngine.Object for example ?
you could try something like this:
Object[] allStuffPossiblyInTheAsset = AssetDatabase.LoadAllAssetsAtPath("Assets/[DragonBuilder]/NewDragon.prefab");
if (allStuffPossiblyInTheAsset != null) {
int nObjects = allStuffPossiblyInTheAsset.Length;
Debug.Log(string.Format("Asset contains {0} object(s)", nObjects));
foreach(Object obj in allStuffPossiblyInTheAsset) {
Debug.Log(string.Format("Asset of type {0}: {1}", obj.GetType().ToString(), obj.ToString()));
}
} else {
Debug.Log("Asset not found");
}
Answer by Chimique · Nov 26, 2014 at 11:03 AM
GameObject temp4 = (GameObject)AssetDatabase.LoadMainAssetAtPath("Assets/[DragonBuilder]/NewDragon.prefab", typeof(GameObject));
Follow this Question
Related Questions
Assets couldn't be loaded 1 Answer
Project moved - how to get back 1 Answer
How can I store and load levels efficiently? 2 Answers
Cannot find 3D Prefab after doing a build. 0 Answers
Lazy Loading and Unloading of prefabs? 0 Answers