Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Undo.RegisterCreatedObjectUndo

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method RegisterCreatedObjectUndo(objectToUndo: Object, name: string): void;
public static void RegisterCreatedObjectUndo(Object objectToUndo, string name);

Parameters

objectToUndo The object that was created.
name The name of the action to undo. Think "Undo ...." in the main menu.

Description

Register an undo operations for a newly created object.

When the undo is performed the object will be destroyed. All newly created objects that are part of undoable state should be registered with this function.

Note: Object destruction works the same way as it does for Object.Destroy (except for the delay). This means that GameObjects will be destroyed along with all their child GameObjects.

#pragma strict
// Creates a new game object as an operation that can be undone
class CreateGameObjectMenu {
	@MenuItem("Example/Create GameObject")
	static function CreateGameObject() {
		// Create GameObject hierarchy.
		var go: GameObject = new GameObject("my GameObject");
		var child: GameObject = new GameObject();
		go.transform.position = new Vector3(5, 5, 5);
		child.transform.parent = go.transform;
		// Register root object for undo.
		Undo.RegisterCreatedObjectUndo(go, "Create object");
	}
}
// Creates a new game object as an operation that can be undone

using UnityEditor; using UnityEngine;

class CreateGameObjectMenu { [MenuItem ("Example/Create GameObject")] static void CreateGameObject () { // Create GameObject hierarchy. GameObject go = new GameObject ("my GameObject"); GameObject child = new GameObject(); go.transform.position = new Vector3 (5, 5, 5); child.transform.parent = go.transform;

// Register root object for undo. Undo.RegisterCreatedObjectUndo (go, "Create object"); } }

Did you find this page useful? Please give it a rating: