I cannot manage to use Undo …

Hi all !

Maybe I’m stupid, but I can’t manage to use Undo … I’ve tried many things, RecordObject, RecordObjects and other things, tried SetDirty, Repaint, etc … Nothing.

This little example doesn’t work … Can you help me ?

using UnityEngine;
using UnityEditor;
using System.Collections;

public class MyWindow : EditorWindow
{
    [MenuItem("Shudrum/My window")]
    public static void ShowWindow()
    {
        EditorWindow thisWindow = EditorWindow.GetWindow(typeof(MyWindow));

        GUIContent title = new GUIContent();
        title.text = "Toolbar";

        thisWindow.titleContent = title;
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 32, 32), "Test")) {
            Undo.RecordObjects(Selection.gameObjects, "Translate");

            foreach (GameObject currentGameObject in Selection.gameObjects) {
                currentGameObject.transform.position += Vector3.forward;
            }
        }
    }
}

Thank you !

I’m replying just so I get alerts on replies to see if you get anything more useful than I have in the past. Good luck with this. Signs of a bad system are:

  1. You can’t find any useful information in the docs
  2. You can’t find any useful information on google
  3. You can’t get any useful information when you post on the official forum

The unity undo system is a bad system. I’ve been trying to get it to work for months and have given up. I’m hoping my asset gets approved without undo functionality because its near impossible to figure out how to get it to work with anything more than the simplest use case.

I believe it’s not working as you’re modifying the “Transform” not the “GameObject”, because of this, unity doesn’t detect any changes.

In your loop try:

Undo.RegisterCompleteObjectUndo(currentGameObject, "Translate");
currentGameObject.transform.position += Vector3.forward;
EditorUtility.SetDirty(currentGameObject);

You could also try Recording the “Transform” instead of the “GameObject”