AssetDatabase.LoadAssetAtPath returning null

Hi, I am working on an editor menu item that simplifies my object placement. (I repeatedly
place the same prefab in the scene en this script makes it easier).

But I keep getting a null return in my Debug.Log and the prefab does not get instantiated in the editor.

I checked if the file existed, it did, I checked if the path was correct, it was correct. All other applications directly find the file except for this editor function.

Any help would be great.

#pragma strict
@MenuItem ("GameObject/Create Other/Spawn",false,-16)

static function SpawnGenerator () {
var path:String= Application.dataPath+"MyAssets/Scripts/FX/Source/Place.prefab";
var prefab: GameObject= AssetDatabase.LoadAssetAtPath(path, typeof (GameObject)) as GameObject;

var ray : Ray= Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
 
       if(Physics.Raycast(ray, hit, 1000)){
         Debug.Log(hit.point);
       }
        
if(prefab){
var Place:GameObject =  GameObject.Instantiate (prefab, hit.point , Quaternion.identity);
}
 Debug.Log(prefab);
  Debug.Log(Place);
 

 
}

If someone will have the same problem as author. You have to use path only as “Assets/SomeFolder/SomeAsset.asset”. You have not add to path Application.dataPath or something else.

I mean:

var path = Application.dataPath + "SomeFolder/SomeAsset.asset";
var asset = AssetDatabase.LoadAssetAtPath<T>(path);

will return null. But:

    var path = "Assets/SomeFolder/SomeAsset.asset";
    var asset = AssetDatabase.LoadAssetAtPath<T>(path);

will work like a charm.

Hello from 2020, we still do the same errors :slight_smile:

Wait, are you trying to load the asset during a Game?
The assetDatabase is only available in the Editor.
If you want to load something during the Game, put it in a Resource-folder and load it with Resources.Load()

At the end i just went on and generated the whole prefab manually trough script.
So instead of instantiating I created an empty object assigning all the components and settings, then put that into the scene. Same result only took a little bit longer to setup.

It seems that AssetDatabase is a little bit bugged on that part.