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 /
  • Help Room /
avatar image
1
Question by lgor · Jan 05, 2017 at 10:03 PM · unity5serializationeditor scripting

Undo.RecordObject doesn't record array

Undo.RecordObject doesn't writes array, so when I press ctrl+z, variable count changes, but array not. Is it okay?

It is simplified example, not real code. Component with variable and array of objects:

  public class DataHolder : MonoBehaviour {
     [SerializeField] public Data[] array = new Data[0];
     [SerializeField] private int count;
 
     public void Reset() {
         array = new Data[0];
         count = 0;
     }
 
     public void AddData() {
         Array.Resize(ref array, array.Length + 1);
         array[array.Length - 1] = new Data();
 
         count++;
     }
 }

In ideal world, count always equals array.Length. But undo operation violates this invariant :( I tried to record whole game object instead of dataHolder, but it doesn't work too.

 [CustomEditor(typeof(DataHolder))]
 public class HolderInspector : Editor {
     public override void OnInspectorGUI() {
         DrawDefaultInspector();
 
         var dataHolder = target as DataHolder;
 
         if (GUILayout.Button("add Data")) {
             Undo.RecordObject(dataHolder, "add data");
             EditorUtility.SetDirty(dataHolder);
 
             //Undo.RecordObject(dataHolder.gameObject, "add data");
             //EditorUtility.SetDirty(dataHolder.gameObject);
 
             dataHolder.AddData();
         }
 
         GUILayout.Label("array size = " + dataHolder.array.Length);
     }
 }

and simple class (really it has a lot of data)

 public class Data {
     [SerializeField] public int value;
 }

Is it normal? How can I save data? I really want to use Undo feature.

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
2

Answer by Adam-Mechtley · Jan 06, 2017 at 07:37 AM

Is your Data class marked with System.Serializable?

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 lgor · Jan 06, 2017 at 10:30 AM 0
Share

Thank you, it solved problem.

avatar image Check_2015 · Feb 19, 2017 at 12:50 PM 0
Share

Thank you!

avatar image viesc123 · Dec 08, 2019 at 02:40 PM 1
Share

I have the same situation, "Data" class is marked as System.Serializable, but still undo does not record changes to individual array fields..

avatar image
0

Answer by warre_engels · Aug 04, 2021 at 09:04 AM

I had the same problem and i already had [System.Serializable] on my class, for me this link helped.

Basically instead of Undo.RecordObject(object, name), you call Undo.RegisterCompleteObjectUndo(object, name) first, then you change the data and then you call EditorUtility.SetDirty(object).

For anyone who still cares after all these years :)

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

100 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

Related Questions

Save/Load Serialization by @Cherno 0 Answers

Unity How to Save Serialized Levels 1 Answer

How can I edit the animation I just created in Unity 5 0 Answers

issue [FAILED BINDER TRANSACTION] android unity3d. JNI ERROR (app bug): accessed stale local reference 0 Answers

How can I get started on making an AI controlled tank for the Tanks Unity tutorial? 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