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
1
Question by deshumake2 · Apr 17, 2020 at 07:13 AM · scriptableobjectsave dataundodirty

Modifying and Saving Scriptable Objects: SetDirty vs. Undo

Hi, I am having some trouble understanding how saving using Undo.* works. I currently have scriptable objects that I am using to pass data between scenes. I am setting some options in the main menu, and I want to use those options in the game. This is my current code to save the object every time it is modified.

 [CreateAssetMenu]
 public class IntVariable : ScriptableObject
 {    
     [SerializeField] private int _variable;
 
     public int Variable
     {
         get => _variable;
         set
         {
             _variable = value;
             // EditorUtility.SetDirty(this);
         }
     }
 }

If I uncomment the SetDirty line, then it all works exactly how I want it to; however, looking online (https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html), SetDirty apparently shouldn't be used anymore, I assumed it was becoming deprecated. Instead of SetDirty, Unity says we are supposed to use Undo.RecordObject(). I tried doing that with this code instead

 public class IntVariable : ScriptableObject
 {    
     [SerializeField] private int _variable;
 
     public int Variable
     {
         get => _variable;
         set
         {
             Debug.Log(EditorUtility.IsDirty(this));
             Undo.RecordObject(this, "Change int");
            
             _variable = value;
             // Undo.FlushUndoRecordObjects();
             Debug.Log(EditorUtility.IsDirty(this));
         }
     }
 }

With debugs to tell if it was actually dirty like it was supposed to be, and it both says it isn't dirty, and when I close and reopen the project, nothing indeed has been saved. Is there something we need to call in order to save these default values, or am I just using the syntax wrong, or am I just going about this whole thing in the wrong way?

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
2

Answer by Wappenull · Sep 22, 2020 at 02:46 PM

This could be answered in 2 completely different topics.


"SetDirty vs. Undo"

.

  • Undo.RecordObject is mostly for scene objects, as written in its document.

  • EditorUtility.SetDirty is for non-scene object. Without undo support. The sole purpose of SetDirty is for editor to believe that this object is dirty and really write it to disk.

  • If you edit your object via SerializedObject way you don't have to call any above. SerializedObject already handle everything. This also happen internally when you edit fields in inspector. Of course you cannot use SerializedObject in game runtime because it is in UnityEditor namespace.


    "I am using (this) to pass data between scenes. I am setting some options in the main menu, and I want to use those options in the game."

.

Looks like you want to pass "game option" around. There is many way to do it. ScriptableObject is NOT one of these way!

.

  • ScriptableObject are mostly for "constant database" not for dynamic data passing. Like database of items, enemy stats, or for storing reference to another asset (UnityEngine.Object). Furthermore, ScriptableObject are only editable and savable in Editor. You wont be able to modify it when build a standalone game. So it is not suitable for your task at all.

  • The first and easiest approach is to use static variable. Make 1 global game state, write to it. Access it from anywhere even scene has changed.

  • If you also want to save them between each game launch, now you probably need PlayerPref.

Comment
Add comment · 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

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

129 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 do I have an EditorWindow save it's data inbetween opening and closing Unity? C# 5 Answers

Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers

Changes to custom serializable object not saved to disk 1 Answer

How to Save and Load a list of Scriptable Objects from a File? 2 Answers

Making a save system in a ScriptableObject using JsonUtility. Are references to prefabs and other ScriptableObjects reliable? 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