Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
9
Question by DrBlock42 · Aug 14, 2015 at 06:06 AM · editorserializationscriptableobject

Scriptable Object resets after Unity is closed

Dear Unity-Comunity,

I have a problem with Scriptable Objects. I generate most of my UI meshes at runtime (like grids, lines, etc.) and I use a Scriptable Object to select the Materials. That makes it a lot easier to quickly change them. My probleme is, that every time I close Unity, the asset (instance of UIData) in my Ressources folder gets reset to these settings: alt text

Best Regards, Théo

My Code:

UIData.cs


 [System.Serializable]
 public class UIData : ScriptableObject {
     [SerializeField]
     public Material Line;
     [SerializeField]
     public float LineThickness;
 
     [SerializeField]
     public Material UnitHighlight;
     [SerializeField]
     public Material WalkArea;
     [SerializeField]
     public Material RunArea;
     [SerializeField]
     public Material Grid;
 }
 

UIDataEditor.cs


 [CustomEditor(typeof(UIData))]
 public class UIDataEditor : Editor {
     private static bool LineFoldout = true;
 
     public override void OnInspectorGUI()
     {
         UIData data = (UIData)target;    
         LineFoldout = EditorGUILayout.Foldout(LineFoldout, "Line");
         if(LineFoldout)
         {
             EditorGUI.indentLevel++;
             data.Line = EditorGUILayout.ObjectField("Line Material", data.Line, typeof(Material), false) as Material;
             data.LineThickness = EditorGUILayout.FloatField("Line Thickness", data.LineThickness);
             EditorGUI.indentLevel--;
         }
         data.UnitHighlight = EditorGUILayout.ObjectField("Unit Highlight Material", data.UnitHighlight, typeof(Material), false) as Material;
         data.Grid = EditorGUILayout.ObjectField("Grid Material", data.Grid, typeof(Material), false) as Material;    
         data.WalkArea = EditorGUILayout.ObjectField("Walk Area Material", data.WalkArea, typeof(Material), false) as Material;
         data.RunArea = EditorGUILayout.ObjectField("Run Area Material", data.RunArea, typeof(Material), false) as Material;
     }
 
     [MenuItem("Assets/Create/UI Data")]
     public static void CreateUIDataAsset()
     {
         Object asset = ScriptableObject.CreateInstance<UIData>();
 
         int i = 0;
         while(true)
         {
             if(AssetDatabase.FindAssets("t:UIData UIData" + i.ToString()).Length == 0)
             {
                 AssetDatabase.CreateAsset(asset, "Assets/UIData" + i.ToString() +".asset");
                 AssetDatabase.SaveAssets();
                 EditorUtility.FocusProjectWindow();
                 Selection.activeObject = asset;
                 break;
             }
             else
             {
                 i++;
             }
         }
     }
 }


inspector.png (11.5 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by stepan-stulov · Aug 14, 2015 at 08:03 AM

You're not marking you scriptable object dirty after changing its values. Thus the values are not scheduled for serialization and thus are not surviving the reload. In the UIDataEditor.OnInspectorGUI you need to EditorUtility.SetDirty(target).

Check this post:

http://answers.unity3d.com/questions/641199/do-i-need-to-use-setdirty-in-my-editors-if-im-usin.html

PS: You don't need to mark public fields as [SerializeField] as publics are already automatically so. Unless of course you do it for the sake of programmer's discipline. My normal workflow is [SerializeField] private fields + a getter + an optionally setter.

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 DrBlock42 · Aug 14, 2015 at 01:19 PM 1
Share

Thank you very much :) Everything is working perfectly now!

avatar image OneSketchyGuy · Oct 09, 2020 at 04:15 AM 0
Share

Thank you! I was having this same issue for a couple of weeks and this was all I needed!

avatar image
1

Answer by eskivor · Nov 22, 2017 at 11:27 AM

I had the same kind of problem, but I already used EditorUtility.SetDirty (); and AssetDatabase.SaveAssets(); but I had a problem using a AssetDatabase.Refresh (); between them (I removed it and it worked then)

Comment
Add comment · Show 1 · 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 melos_han_tani · Nov 11, 2021 at 05:32 AM 0
Share

Removing AssetDatabase.Refresh() fixed this issue for me. I was loading a scriptableobject (which had a single asset, which was ONLY edited through the Inspector, outside of Play Mode.) On my computers, the Asset would survive Unity restarts / computer restarts, but for some reason, even though I was NOT modifying the scriptableObject in any scripts (only reading its data), another developer, when getting the updated Asset through git, Unity would reset the asset's (public) properties.

more details if anyone wants it https://forum.unity.com/threads/scriptableobject-asset-gets-reset-by-other-team-member-2019-4-2f1.1196113/

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer

Serialization depth limit 7 exceeded 1 Answer

Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers

pass child class to ScriptableObject.CreateInstance<> ? 1 Answer

Why does the value of the public property of [Serializable] class persist in ScriptableObject between Editor runs ? 2 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