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 · Nov 06, 2017 at 08:21 PM · listdatabasescriptableobjectitems

ScriptableObject List doesn't save

Edit: So I have found out that EditorUtility.SetDirty() is obsolete. How do I save it then?

I am making a Item database, inspired by BurgZerg, and for some reason, my list in the Database doesn't save after unity reload or it erases all data after I save a the ItemDatabase.cs even tho I have EditorUtility.SetDirty() after every change.

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using UnityEditor;
 
 public class ItemDatabase : ScriptableObject {
 
     [SerializeField]
     List<Item> items = new List<Item> ();
 
     public void Remove(Item item) {
         items.Remove (item);
         EditorUtility.SetDirty(this);
     }
 
     public void RemoveAt(int index) {
         items.RemoveAt (index);
         EditorUtility.SetDirty(this);
     }
 
     public void Add(Item item) {
         items.Add (item);
         EditorUtility.SetDirty(this);
     }
 
     public int CountItems() {
         return (int)items.Count;
     }
 
     public Item Item(int index) {
         return items.ElementAt (index);
     }
 
 }
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
Best Answer

Answer by FlightOfOne · Nov 06, 2017 at 09:36 PM

Take a look here: https://answers.unity.com/questions/831285/can-you-use-a-serializedobject-to-save-game-state.html

Take a look at all the answers there, maybe one will give you a clue.

Comment
Add comment · Show 4 · 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 Darkhalo45 · Nov 06, 2017 at 09:41 PM 0
Share

The link says I should serialize the List, but it doesn't explain how should I do that. I'm very new to serialization, so still kinda stuck.

avatar image Darkhalo45 · Nov 06, 2017 at 09:47 PM 0
Share

Ok, I got it to work now, just had to google more about Serialization. What fixed the problem was that I just made my Item class [System.Serializable]. Also, all variables in the Item class had to be made to [SerializeField].

avatar image FlightOfOne Darkhalo45 · Nov 06, 2017 at 10:38 PM 0
Share

I think it depends on what what method you use to serialize (xml, json, binary formatter etc..). Some allow allow private fields, and some only allow, public, non properties. But from my experience, almost all of them work if it's public field.

I use scriptable objects a lot but I have never really used them for saving data. I always use xml/jason/binary. i think it's worth exploring Json and X$$anonymous$$L. I think it would be a good investment. It's not that difficult really.

   public void SaveJsonData(T _data, string _fullPath)
     {
         string _json= JsonUtility.ToJson(_data);
         File.WriteAllText(_fullPath, _json);
         Debug.Log("<color=black>-Json- File Saved to: [" + _fullPath+"]</color>");
     }
  public T LoadJsonData(string _fileName, string _ext = ".cfg", string _path = "")
     {
 
         if (_path == "")
         {
             _path = Application.persistentDataPath + "/";
         }
 
         _path = _path + _fileName + _ext;
         if (!File.Exists(_path))
         {
             Debug.LogError("Not Found: [" + _path + _fileName + _ext + "]");
             return default(T); //returns a null for reference type and 0 for values
         }
 
         T _deserializedData = JsonUtility.FromJson<T>(File.ReadAllText(_path));
         Debug.Log("-Json- File Loaded: " + _path + _fileName + _ext);
 
         return _deserializedData;
     }
 



avatar image FlightOfOne FlightOfOne · Nov 06, 2017 at 10:40 PM 0
Share

https://unity3d.com/learn/tutorials/topics/scripting/serialization-and-game-data

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

77 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

Related Questions

A node in a childnode? 1 Answer

Auto removing entries form a scriptableobject list when asset is deleted. 0 Answers

Scriptable Object's Data Gets Lost After Re-opening Unity!!! 1 Answer

Saving Data (closed) 1 Answer

how to split TextAsset into a Dictionary? 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