• 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 /
This question was closed Feb 16, 2019 at 01:26 PM by SharkoFR for the following reason:

On a répondu à la question, la bonne réponse a été acceptée

avatar image
0
Question by SharkoFR · Feb 16, 2019 at 01:01 PM · assetdatabase

How can i save my .asset file.

Hey! my asset file revert when i start the scene or restart the project, here is the code:

I removed some inutile code. (all this code is in an editor window)

         SMP2_store Storage;  
     
       void OnEnable()
         {
     
             Storage = (SMP2_store)Resources.Load("store.asset") as SMP2_store;
     
             if (Storage == null)
             {
     
                 Storage = CreateInstance<SMP2_store>();
                 AssetDatabase.CreateAsset(Storage, "Assets/SaveMePro 2/Resources/store.asset");
                 AssetDatabase.SaveAssets();
                 AssetDatabase.Refresh();
             }
     
     }
     
         private void OnGUI()
         {
 
   if (GUILayout.Button("Save", GUILayout.MaxWidth(150)))
         {
                 EditorUtility.SetDirty(Storage);
                 AssetDatabase.SaveAssets();
                 AssetDatabase.Refresh();
                 saved = true;
             }
 }
     }
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 SharkoFR · Feb 16, 2019 at 01:05 PM 0
Share

I just tried to close my editor window before starting the scene and the asset is well saved, so the problem come from the OnEnable void, i think it create a new asset all times the window is enabled, any suggestion to resolve it?

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by SharkoFR · Feb 16, 2019 at 01:26 PM

Solved it by replacing

  Storage = (SMP2_store)Resources.Load("store.asset") as SMP2_store;

by

  Storage = (SMP2_store)Resources.Load("store") as SMP2_store;
Comment
Add comment · Show 2 · 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 Bunny83 · Feb 16, 2019 at 01:32 PM 0
Share

I actually wrote this answer but you closed your question too quickly:


This line:

 Storage = (S$$anonymous$$P2_store)Resources.Load("store.asset") as S$$anonymous$$P2_store;

will always fail since resource paths must not include the file extension. So it should be


 Storage = Resources.Load<S$$anonymous$$P2_store>("store");


Note you actually double casted your return value which makes no sense at all. Also be aware that an "as cast" may hide a type issue as you don't get any errors if the returned type can not be casted but the as cast simply returns null. You really want to avoid the as cast in such cases. The generic method is the better choice anyways since it will use the provided type in the search as well.

avatar image SharkoFR Bunny83 · Feb 16, 2019 at 03:25 PM 0
Share

So thanks you for your help :) And also thanks for the double cast warn.

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

99 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 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 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 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 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

How are unity references serialized? (instanceID related) 1 Answer

ObjectField Get object type(Asset, Scene Object) 2 Answers

I don't know how to use AssetDatabase.GetTextMetaDataPathFromAssetPath() 0 Answers

How to move an asset to a newly created directory in the AssetDatabase ? 3 Answers

How do I return the path of a sub-asset in an AssetDatabase ? 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