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 Darkhalo45 · Feb 01, 2018 at 07:20 PM · serializationscriptableobjecteditorwindow

ScriptableObject does not serialize

For some reason, my ScriptableObject does not save the list I have on it. I am trying to make a database. I tried everything to fix it, even copied the code from my older project(SO work properly there), but still nothing happens. This is the ScriptableObject script:

 public class TestEnemyDatabase : ScriptableObject {
 
     [SerializeField]
     List<TestEnemyScript> database = new List<TestEnemyScript>();
 
     public void Add(TestEnemyScript enemy) {
         database.Add (enemy);
     }
         
 }


Item class:

 [System.Serializable]
 public class TestEnemyScript {
 
     [SerializeField]
     int _damage;
 
     [SerializeField]
     float _health;
 
     public TestEnemyScript(int dmg, float hp) {
         _damage = dmg;
         _health = hp;
     }
 
     public int Damage {
         get { return _damage; }
         set { _damage = value; }
     }
 
     public float Health {
         get { return _health; }
         set { _health = value; }
     }
 
 }

And the Editor Script:

 public class TestDatabaseEditor : EditorWindow {
 
     int newDamage;
     float newHealth;
 
     private TestEnemyDatabase enemyDatabase;
 
     private const string DATABASE_PATH = "Assets/TestDatabase.asset";
 
     [MenuItem("Window/Test Enemy Database")]
     public static void ShowWindow() {
         EditorWindow window = EditorWindow.GetWindow (typeof(TestDatabaseEditor));
         window.Show ();
     }
 
     void OnEnable() {
         if (enemyDatabase == null) {
             LoadDatabase ();
         }
     }
 
     void LoadDatabase() {
         enemyDatabase = (TestEnemyDatabase)AssetDatabase.LoadAssetAtPath (DATABASE_PATH, typeof(TestEnemyDatabase));
 
         if (enemyDatabase == null) {
             CreateDatabase ();
         }
     }
 
     void CreateDatabase() {
         // Debug.Log ("New Enemy Database has been created.");
 
         enemyDatabase = ScriptableObject.CreateInstance<TestEnemyDatabase> ();
         AssetDatabase.CreateAsset (enemyDatabase, DATABASE_PATH);
         AssetDatabase.SaveAssets ();
         AssetDatabase.Refresh ();
     }
 
     void OnGUI() {
         newDamage = EditorGUILayout.IntField ("Damage: ", newDamage);
         newHealth = EditorGUILayout.FloatField ("Health: ", newHealth);
 
         if (GUILayout.Button("Add a new item")) {
             // TestEnemyScript enemyScript = new TestEnemyScript (newDamage, newHealth);
 
             enemyDatabase.Add (new TestEnemyScript (newDamage, newHealth));
         }
     }
 }

Comment
Add comment · Show 5
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 PizzaPie · Feb 01, 2018 at 08:28 PM 0
Share

On the editor script after line 46 add EditorUtility.SetDirty(enemyDatabase);

avatar image Darkhalo45 PizzaPie · Feb 01, 2018 at 08:59 PM 0
Share

It works! Thak you, but wasn't SetDirty() obsolete before??

avatar image PizzaPie Darkhalo45 · Feb 02, 2018 at 10:24 AM 0
Share

Well, there is one inside another class, i think Editor.SetDirty() , not sure though, which is obsolete.

Show more comments
avatar image Tom507 · Feb 02, 2018 at 03:00 PM 0
Share

maybe try using public variables inse$$anonymous$$d of serialized fields they are automatically serialized

0 Replies

· Add your reply
  • Sort: 

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

81 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

Related Questions

Save EditorWindow to disk 1 Answer

Serialization depth limit 7 exceeded 1 Answer

Saving players progress in ScriptableObject asset 1 Answer

Can I use Scriptable Objects for Hero Stats that change throughout the game? 0 Answers

Make the same serialized class as a ScriptableObject? 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