Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MarkD · Jan 01, 2014 at 05:47 PM · editorload

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);
  
 
  
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkD · Oct 23, 2020 at 03:16 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkD · Jan 01, 2014 at 06:03 PM 0
Share

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).

avatar image HappyMoo · Jan 01, 2014 at 06:07 PM 0
Share

"Assets", not "Asset"

avatar image HappyMoo · Jan 01, 2014 at 06:08 PM 0
Share

do you have a myassets folder in your assets or is this supposed to be the assets folder?

avatar image MarkD · Jan 01, 2014 at 06:43 PM 0
Share

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).

avatar image HappyMoo · Jan 01, 2014 at 06:59 PM 0
Share

edited Answer

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges