Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PijusVose · Dec 30, 2017 at 09:24 AM · serializationscriptableobjectserialize

ScriptableObject loses data after Reload

I have made database of the Enemies class, then I have a EditorWindow where I add and edit my Database, but whenever I close and open my Unity, all data is gone! But when I play and stop my game, the data is still there tho. I have serialized all classes that I needed, made and saved my ScriptableObject as an Asset, but still all my data is lost after Unity is closed.

 [System.Serializable]
 public class EnemyDatabase : ScriptableObject {
 
     [SerializeField] public List<Enemy> database;
 
     void OnEnable() {
         if (database == null) {
             database = new List<Enemy> ();
         }
     }
 }

Here is the Enemy class:

 [System.Serializable]
 public class Enemy {
 
     [SerializeField] public string enemyName;
     [SerializeField] public int id;
     [SerializeField] public int damage;
     [SerializeField] public int points;
     [SerializeField] public float speed;
     [SerializeField] public float chance;
     [SerializeField] public GameObject prefab;
 
     public Enemy(string name, int newId, int dmg, int pnts, float vel, float rarity, GameObject go) {
         enemyName = name;
         id = newId;
         damage = dmg;
         points = pnts;
         speed = vel;
         chance = rarity;
         prefab = go;
     }
 
 }

And the EditorWindow script, where I create a new Database if there isn't one, and then save it as an Asset:

     void CreateDatabase() {
         enemyDatabase = ScriptableObject.CreateInstance<EnemyDatabase> ();
         AssetDatabase.CreateAsset (enemyDatabase, DATABASE_PATH);
         AssetDatabase.SaveAssets ();
         AssetDatabase.Refresh ();
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by x4637x · Dec 30, 2017 at 11:08 AM

From your "EditorWindow " script you are only creating new "enemyDatabase" and not loading the existing one?

Have you exposed your "enemyDatabase" variable in your inspector? If you have, you could drag and drop your saved asset to the field and it will be loaded.

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 PijusVose · Dec 30, 2017 at 09:12 PM 0
Share

I am loading the existing one. $$anonymous$$y enemyDatabase is a private field and not serialized.

avatar image x4637x PijusVose · Jan 02, 2018 at 09:14 AM 0
Share

You could debug this by setting up EnemyDatabase.name for your saved asset. And have the console print out its name everytime you trying to use it. So you could find out from where it starts to get lost.

And the name attribute should be inherited from UnityEngine.Object class so you don't need to create your own.

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

76 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

Related Questions

List of ScriptableObjects lost on project reload 1 Answer

Saving Game Confussion 1 Answer

[Solved]How to serialize Dictionary with Unity Serialization System 6 Answers

Saving Non-Prefab Data During Editor Play 0 Answers

Difference between assigning a value in inspector and with a custom editor script 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