Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by Dynamite3D · May 20, 2017 at 07:19 PM · editorwindowundo

Undo.RecordObject doesn't work

Hello fellows!

I've watch several threads about this topic, but none of the solutions works for me :(

I just want to create and edit an Object via an editor window, and make ctrl+z works if I change it. I don't mean a GameObject, just and Object. This is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class UndoLearning1 : EditorWindow {
     Tree newTree = new Tree();
 
     [MenuItem ("Tools/Undo Learning 1")]
     public static void Init(){
         UndoLearning1 myWindow = (UndoLearning1)EditorWindow.GetWindow(typeof (UndoLearning1));
         myWindow.Show();
     }
 
      void OnGUI(){
         if (GUILayout.Button("Create Branch")){
             Undo.RecordObject(newTree, "Branch Added");
             newTree.branchs.Add(new Branch());
             newTree.branchs[0].points.Add(Vector3.zero);
         }
      }
 
      void Update(){
          Debug.Log(newTree.branchs.Count);
      }
 }
 
 public class Tree : Object{
     public List<Branch> branchs = new List<Branch>();
 }
 
 public class Branch{
     public List<Vector3> points = new List<Vector3>();
 }

When I hit ctrl+z after "Create Branch" button, the console says "Undo object may not be null." But the Debug.Log works fine and logs what it should.

What's wrong with this code?

Thanks for your time!

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
2

Answer by UH4631 · Feb 13, 2019 at 06:28 AM

This is just a hunch. But I believe that your problem may be due the fact that Branch is not set as serializable. Try using the the [System.Serializable] attribute on the Branch class.

I had a problem similar to this earlier where I was modifying a private member variable through a property. Private variables are not serialized by default in unity so I had to tell unity to both serialize it and hide it in the inspector using attributes ([SerializeField] and [HideInInspector])

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 HarryBGamer2570 · Mar 19 at 04:12 PM 0
Share

Dude totally, thank you, the "[SerializeField] [HideInInspector]" strategy works!!

avatar image
1

Answer by MarshCZA · Jun 30, 2017 at 02:46 PM

Perhaps try using Undo.RegisterCreatedObjectUndo instead

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 Dynamite3D · Jun 30, 2017 at 03:08 PM 0
Share

Thank you! But that's for a different purpose I'm afraid. I want to restore a previous state of an object, not undo an object creation. But thanks anyway :D

avatar image
1

Answer by IgorAherne · Jul 19, 2017 at 10:13 PM

I found this post by complete accident:

quoting it in case it's deleted:

Undo.RegisterCompleteObjectUndo(Object objectToUndo, string name) will record a copy of the full state of the object that it will keep, unlike Undo.RecordObject(Object objectToUndo, string name) that will only keep a copy of the state until the end of the frame to compute a diff.

.

By the way, I just got the following error that drove me to find more on this method:

Generating diff of this object for undo because the type tree changed. This happens if you have used Undo.RecordObject when changing the script property. Please use Undo.RegisterCompleteObjectUndo So RegisterCompleteObjectUndo seems important in complex situations were a diff is not possible.

Couple of words from myself:

Undo.RegisterCompleteObjectUndo seems to work with Event and editor-GUI as well every time. And RecordObject seemed only to work sometimes (only worked occasionally)

Additionally, if you are working with Event in editor script, and actually have code that Use()s the GUI event, chances are the event might not get through to unity's undo system. You might need to call Undo.FlushUndoRecordObjects(); after RegisterCompleteObjectUndo()

.

Don't forget to use EditorUtility.SetDirty() on the object, AFTER you've recoreded into Undo

Here is an example, allowing me to offset 2D viewport up/down left/right:

 void DragViewingArea() {
             int id = GUIUtility.GetControlID(6, FocusType.Passive);
  
             //prepare for drag:
             if (_currentEvent.type == EventType.MouseDown && _currentEvent.button == 2) {
                 Undo.RegisterCompleteObjectUndo(_fsm, "begin drag FSM viewport on " + _fsm.gameObject.name);
                 Undo.FlushUndoRecordObjects();
              
                 GUIUtility.hotControl = id;
                 _currentEvent.Use();
                 return;
             }
  
             //drag:
             if(_currentEvent.type == EventType.MouseDrag && GUIUtility.hotControl == id) {
                 _fsm._fsmEditorWindow_TableOffset += _currentEvent.delta;
                 _currentEvent.Use();
                 EditorUtility.SetDirty(_fsm); //set dirty as soon as we actually drag
  
                 return;
             }
  
             //finished dragging
             if (_currentEvent.type == EventType.mouseUp  &&  _currentEvent.button == 2  &&  GUIUtility.hotControl == id) {
                 GUIUtility.hotControl = 0;
                 _currentEvent.Use();
                 return;
             }
  
         }


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 Dynamite3D · Jul 22, 2017 at 08:02 PM 0
Share

Hello! Thank you for this good explanation. But it seems like the function you say is from Unity 4, and it doesn't exist on Unity 5. Am I right?

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

107 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

Related Questions

Undo Redo in Graph View 0 Answers

Tracking changes of a Component on the selected GameObject in custom EditorWindow 1 Answer

How to Undo a bool on an EditorWindow 1 Answer

Is it possible to access Layer visibility and lock status via script? 1 Answer

GetWindow vs GetWindowWithRect 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