- Home /
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);
}
Answer by Last_Imba · Oct 23, 2020 at 11:48 AM
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 :)
I had completely forgotten about this post! In the end, after a while, I did find the solution you suggested.
Damn 6 years ago, I feel old now.
I am changing the accepted answer to this one as it contains actual solution.
Answer by HappyMoo · Jan 01, 2014 at 06:00 PM
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()
http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html
Application.Datapath replaces the first "Asset" I tried it with Asset before, no result, Debugger tells me the file exists, when loaded into a Gameobject like above it returns me null.
Also losing .prefab is no solution either since the file has to be an accurate path (and i tested it before also with no result).
do you have a myassets folder in your assets or is this supposed to be the assets folder?
I have a $$anonymous$$yAssets folder inside my asset folder, it dirrives the main assets for my game, it is to keep all the plugins and asset store imports organized outside of my game folder. (Assets/$$anonymous$$yAssets is the path).
Answer by MarkD · Jan 01, 2014 at 10:22 PM
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.
Your answer
Follow this Question
Related Questions
Is there any way to call OnLevelLoaded when loading a scene in editor? 2 Answers
Using EditorUtility.OpenFilePanel outside editor? 1 Answer
Perform action on save/load in editor 2 Answers
Equivalent of OpenFilePanel without using UnityEditor? 5 Answers
SceneManager.LoadSceneAsync freeze loading scene in editor. 0 Answers