AssetDatabase.LoadAssetAtPath not working?

I have the following line in my script:

var somePrefab : GameObject = AssetDatabase.LoadAssetAtPath("Assets/SomeFolder/thePrefab.prefab", typeof(GameObject));

This always returns null for me…I’ve been digging around the 'net and trying all sorts of different methods, but nothing works yet. Please help? :frowning: So frustrated!

is the rest of this editor function fine? (it will only work in the editor, for runtime you must use Resources and you can NOT use LoadAtPath at all)

Yep, everything else works fine. Have all sorts of lovely things working (lol, first time doing editor scripting, loving the flexibility), but this one thing will not budge.

Definitely is only in editor, not runtime.

Any other thoughts? Thanks!

1 Like

I fear no, not basing on the informations I know at least

1 Like

Just a thought, don’t know if this will work… but in an editor script I wrote I use that function. For the second parameter (looking through my code) I simply have “GameObject,” no typeof.

Give that a shot

1 Like

Another idea is the path to the asset is misspelled. Are you sure you spelled it right?

Will do, thanks!

Unfortunatly, yes :frowning: I checked that about 15 times, haha. Thanks though!

Oh wait, I already tried it with no “typeof”, no luck :frowning: Errg. I just don’t see why this isn’t working. It’s the final piece of a really helpful script I’ve got going, but no joy :frowning:

Did you know that there are multiple ways to load assets? Here’s just a few:

EditorGUIUtility.Load ();
EditorGUIUtility.LoadRequired ();
Resources.LoadAssetAtPath ();

Have you tried all of them?

Thanks! I’ll give that a try :slight_smile:

Are you on Mac or PC? If Mac, be aware that paths are case-sensitive.

Try using unifycommunity.com to verify you have the correct path and name for the asset you’re trying to load.

Thanks SO much Daniel, Resources.LoadAssetAtPath() did the trick!! :smile: Wee!

When my super-awesome-neato plugin is released, please remind me that I owe you a copy :wink:

(ps also thanks Superpig and fhmiv, appreciate the suggestions :slight_smile: )

This can be of some help. It loads all assets of some type (in my case, material):

string[ ] assetPaths = AssetDatabase.GetAllAssetPaths();
string s = assetPaths.Length + " assets:\n";
int counter = 0;
foreach (string path in assetPaths)
{
Object asset = Resources.LoadAssetAtPath(path, typeof(Material));
if (asset != null)
{
counter++;
s += asset.ToString() + “\n”;
}
else
s += path + " IS NOT A MATERIAL\n";
}

Debug.Log(counter + " materials out of " + s);

isn’t this the other way around?

This may be too late, but replace your line:

With:

5 Likes

GabeF your sollution did the trick! Thanks!

Met the same problem. It came up when I did a copy of asset folder with AssetDatabase.ImportAsset(newAssetPath);. Strangely that the assets in the newpath were not contained in the AssetDatabase! (Thanks to roberto_sc’s advice of checking the AssetDatabase!)

So I just fixed it by:

AssetDatabase.ImportAsset(newpath);
var asset = AssetDatabase.LoadAssetAtPath(newpath, typeof(xxx));
2 Likes

that worked for me @dbdenny , thanks!