• 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
2
Question by Ming · Oct 14, 2011 at 01:01 AM · scriptableobjectassetdatabase

AssetDatabase.LoadAssetAtPath() doesn't always work

AssetDatabase.LoadAssetAtPath() seems to only work after AssetDatabase.CreateAsset() is first called. But doing so would destroy the Asset that is already there. If I call AssetDatabase.LoadAssetAtPath() immediately after loading the Unity editor, it does not return the full data. Can anyone see what I'm doing wrong in the code sample below?

 using UnityEngine;
 
 using UnityEditor;
 
 
 
 [System.Serializable]
 
 public class AssetTest : ScriptableObject
 
 {
 
     public         UIObjectSerializedData m_UIObjectSerializedData;
 
     public        string    m_OtherString;
 
     
 
     [System.Serializable]
 
     
 
     public class UIObjectSerializedData
 
     {
 
         public     string     m_NameString;
 
         public    int        m_IntTest;
 
     }
 
 
 
     public static void CreateAsset(AssetTest asset)
 
     {
 
         Debug.Log("CreateAsset: " + asset.name + "\n");
 
         AssetDatabase.CreateAsset(asset, "Assets/AssetTest.asset");
 
     }
 
     
 
     public static AssetTest LoadAsset(string assetPath)
 
     {
 
         var  asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(AssetTest));
 
         Debug.Log("LoadAsset: " + assetPath + "=" + asset.name + "\n");
 
         return asset as AssetTest;
 
     }
 
     
 
     public string ShowData()
 
     {
 
         string outString = m_OtherString + ", " + m_UIObjectSerializedData.m_NameString + ", " + m_UIObjectSerializedData.m_IntTest.ToString();
 
         return outString;
 
     }
 
 }
 
 
 
 public class AssetTestMenu
 
 {
 
     static string AssetTestPathName = "Assets/AssetTest.asset";
 
     [MenuItem("AssetTest/Test Create")]
 
     static void TestCreateMenu()
 
     {
 
         AssetTest asset = ScriptableObject.CreateInstance<AssetTest>();
 
         asset.name = "MytestAsset";
 
         AssetTest.CreateAsset(asset);
 
         asset.m_OtherString = "OtherString";
 
         asset.m_UIObjectSerializedData.m_NameString = "This space intentionally left blank.";
 
         asset.m_UIObjectSerializedData.m_IntTest = 12345;
 
         
 
         AssetDatabase.SaveAssets();
 
         
 
         //    try loading it right away to see if we got the right data
 
         Debug.Log("Test Create: NameString=" + asset.ShowData() + "\n");
 
         TestLoadMenu();
 
     }
 
     
 
     [MenuItem("AssetTest/Test Load")]
 
     static void TestLoadMenu()
 
     {
 
         var loadedObj = AssetTest.LoadAsset(AssetTestPathName);
 
         AssetTest asset = loadedObj as AssetTest;
 
         Debug.Log("Test Load: NameString=" + asset.ShowData() + "\n");
 
     }
 
 }

Output If AssetTest/Test Create is selected:

 CreateAsset: MytestAsset
 
 Test Create: NameString=OtherString, This space intentionally left blank., 12345
 
 LoadAsset: Assets/AssetTest.asset=AssetTest
 
 Test Load: NameString=OtherString, This space intentionally left blank., 12345

If AssetTest/Test Load is selected without first choosing AssetTest/Test Load:

 LoadAsset: Assets/AssetTest.asset=AssetTest
 
 Test Load: NameString=, , 0
Comment
Add comment · Show 1
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 chuckrussell · Apr 12, 2012 at 03:16 PM 0
Share

,I am also having this problem. Have you come up with a solution to it? In my case the object which has been created comes back out, and some of the fields are populated and others are null. In terms of your class structure, the m_NameString variable is populated but the m_IntTest is null.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jahroy · Oct 14, 2011 at 01:40 AM

Are you positive that an instance of type AssetTest exists at that path?

Has it been instantiated and does it contain valid data?

I notice that AssetTest is a ScriptableObject. Is AssetTest/AssetTest.asset the location of the script or the location of an instance of that type?

To create an instance of a ScriptableObject you can call ScriptableObject.CreateInstance. This creates an actual instance of whatever type you specify.

Sorry if you already knew this... Just trying to help.

I'm assuming you already understand this, since otherwise you'd be erasing your script when you create a new Asset.

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
avatar image
0

Answer by Ming · Apr 12, 2012 at 05:04 PM

I believe there is a bug in Unity where if you have a class like this:

 public class UIObjectSerializedData
 {

    public    string    m_NameString;

    public    int       m_IntTest;

 }

that you serialize, then only the LAST variable of that class is actually serialized rather than the entire class! You can test this by switching the order of m_NameString and m_IntTest in the class.

I worked around this by not having a class serialized, but by simply taking the members out of that class and putting them in the class above it and serializing the field directly.

It would be nice if the class serialized properly. This would make my architecture somewhat cleaner. But for now, this seems to be an acceptable workaround.

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
avatar image
0

Answer by chuckrussell · Apr 12, 2012 at 05:42 PM

Actually mine was a result of unity not being able to serialize a System.Object type.

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
avatar image
0

Answer by IgorAherne · Dec 03, 2016 at 09:47 AM

I was too lazy to read question, but this might help

 if (EditorApplication.isCompiling) {
             return;
 } 
 //only do loading from asset database / re-creating assets if not compiling.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

AddObjectToAsset (reference) becomes null 1 Answer

Why unity can't create/save asset (ScriptableObject ) at runtime ? 1 Answer

Create script from template, then create .asset file from new script 1 Answer

Converting XML-files into assets 1 Answer

Scriptable Object references if not saved in AssetDatabase? 1 Answer


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