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
6
Question by Statement · Feb 15, 2010 at 10:01 AM · editorinspectorassetserializationscriptableobject

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

Solution

Call EditorUtility.SetDirty(yourObject); on any changes made to it.


I have a problem where a ScriptableObject doesn't save its members properly when edited through a custom EditorWindow. It saves properly when edited through the inspector. I can see through the inspector that the ScriptableObject is affected properly by the EditorWindow.

Am I required to explicitly call some SaveAsset() function on the object when editing it through the EditorWindow? Either that, or it might be some bug in Unity. In the former case, does anyone know how I can save the ScriptableObject? In the latter case, does anyone know of a workaround?

Further, the ScriptableObject contains two ints for width and height, and a one-dimensional array of a custom enum type (basically a flattened 2d array). Both the ScriptableObject and the the custom enum type uses System.Serializable attribute on the class and enum type. All fields are public. No properties are being used. The ScriptableObject also exists in the project, so it is not a transient object.


Currently I am trying to force-save the asset through AssetDatabase.SaveAssets. This approach doesn't seem natural as I don't want to necessarily save all assets, only the one I am editing.

However, as I found out after some trial and error AssetDatabase.SaveAssets doesn't actually save the asset as expected.

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

3 Replies

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

Answer by Lucas Meijer 1 · Feb 15, 2010 at 11:33 AM

You need to call EditorUtility.SetDirty() on your scriptable object.

Comment
Add comment · Show 6 · 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 Statement · Feb 15, 2010 at 12:24 PM 0
Share

That did the trick! (Apparently ScriptableObject.SetDirty also works although it is obsolete.) EditorUtility.SetDirty it is!

avatar image spectre1989 · Aug 01, 2013 at 11:10 PM 0
Share

Dude.. I love you.

avatar image Pawl · Jun 25, 2014 at 10:11 PM 1
Share
  • This works great. It's worth noting that if you're calling this from an EditorWindow on the Selection.gameObjects array for example, you need to pass the component you modified to SetDirty() and not the gameObject.

avatar image sumeetkhobare · Jun 15, 2015 at 12:17 AM 0
Share

This doesn't work for two-dimensional array. Doesn't anyone know how to fix that? I am using Unity 5.

avatar image ZenMicro · Jan 03, 2016 at 01:41 PM 0
Share

This certainly did do the trick, I was calling AssetDatabase.SaveAssets(); in the if (GUI.changed) {} which is what the tutorials from unity had me believe (not GUI.changed but the save)... right next to the other Ondirty call... I guess this is specific to the editor window. Anyway wasted a couple of days reworking my data structures as some were showing up as Type $$anonymous$$ismatch, again this probably wasn't helping... but the stars have aligned :) thanks Lucas.

Show more comments
avatar image
2

Answer by bodowens · Nov 01, 2017 at 08:13 AM

I tried EditorUtility.SetDirty(...), Undo.RecordObject(...), SerializedObject.Update(), SerializedObject.ApplyModifiedProperties(), none of them have any effect. The only thing that did have effect is AssetDatabase.SaveAssets(), but that causes horrible stutter.

I thouth that maybe Unity only saves modified assets on exit, so I tested that too. It didn't work.

Comment
Add comment · Show 3 · 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 dustin_pau1 · Jan 06, 2018 at 12:10 PM 0
Share

Have you found a solution? I seem to be having the same problem. I have tried EditorUtility.SetDirty(), AssetDatabase.SaveAssets(), Undo.RecordObject(), and none seem to be working.

avatar image MoJoe_Blue_ · Jan 15, 2018 at 05:43 PM 0
Share

@dustin_pau1 Have you found a solution ? I'm getting the same problem...

avatar image eskivor · Apr 02, 2018 at 04:17 PM 1
Share

For me it finally worked when I used EditorUtility.SetDirty(...) without using AssetDatabase.SaveAssets()

avatar image
0

Answer by ecesis_llc · Feb 16, 2016 at 12:32 AM

I know this is answered, but I came across this topic while trying to resolve a similar issue. If you are making edits to non-unity objects, like custom structs, but the change is not reflected you may want to use Repaint().

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

8 People are following this question.

avatar image avatar image 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

How should I serialize data that is also editable in the Inspector? 2 Answers

How to make a serialized editor only field 2 Answers

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

Serializing list of base and derived classes with custom inspector 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