- Home /
Understanding The Undo Function (JavaScript)
Heyo!
I am trying to use the undo function in Unity, but can't get it to work. I have been looking at the ScriptReference but there's a lot of superfluous info making it really difficult for me to understand.
How would I go about performing the most basic Undo?
Where one function registers the entire scene, saving it how it was. Then another function performs the Undo, reverting the scene back to that saved state.
Whenever I try to use Undo.RegisterSceneUndo(), I get the error message: 'RegisterSceneUndo' is not a member of 'Undo' I am clearly doing something wrong?
Cheers for the help, Magnus :)
I think you mean JavaScript. Java is an entirely different language, not that anyone would misunderstand you here, since there are only 3 possible languages you could be talking about. Also, this problem isn't necessarily a language specific problem, I think, just pointing it out.
Are you using the latest version of Unity? Sometimes people use older versions of Unity, but read from the up to date script reference on the website. Some functions and classes were not there from the beginning and were added sometime along the way.
What version of Unity are you using?
Is the error inside monodevelop? (or whatever IDE you use)
Also, please paste some of the code that includes the call to Undo.RegisterSceneUndo()
Undo.RegisterSceneUndo() seems to need a string parameter.
Undo.RegisterSceneUndo("Name of undo save")
This is in a script that inherits from Editor, right?
http://docs.unity3d.com/Documentation/ScriptReference/Editor.html
Answer by Bunny83 · Sep 22, 2013 at 11:25 AM
The Undo class is an editor class and can only be used inside the Unity editor. Usually there's a note on the doc page but it seems they forgot it. However if you search the clas in the left class-list you will find the Undo class in the editor-classes-list.
In short you can't use this class in your game.
Oh right, I get ya! So its a similar deal to pressing Ctrl Z in the Editor?
$$anonymous$$an that's so annoying. Is there any way of perfor$$anonymous$$g an Undo function in Runtime then?
Unity doesn't provide such a functionality at runtime. You either save the previous state yourself or might want to use the UnitySerializer.
Since it's your game you know best what can be changed by the user and what you want to be revertable. This might be the position / rotation / scale of certain objects or your some custom variables of your own scripts.
UnitySerializer looks like it will do the trick. I will give that a go, thanks :)
Answer by kasplask · Oct 31, 2013 at 01:24 AM
UnitySerializer is great, i really applaud whydoidoit for his efforts, but it does introduce some (at least to me) unwanted noise. Another option, something that works out great for me, is to make a clone of the object that you're performing an action on, storing its transform values, making it a child of a GameObject that you've hidden out of the way and then disabling it. You can then undo the action of the object by destroying the current object and replacing it with the clone.
```javascript // A custom action class public class Action { var originalObject : GameObject; var storedObject : GameObject; var storedPosition : Vector3; var storedRotation : Vector3; var storedScale : Vector3;
function Action ( o : GameObject ) {
originalObject = o;
storedObject = MonoBehaviour.Instantiate ( originalObject );
storedObject.name = storedObject.name.Replace("(Clone)","");
storedPosition = originalObject.transform.position;
storedRotation = originalObject.transform.localEulerAngles;
storedScale = originalObject.transform.localScale;
}
}
// Needed vars public var actions : List.< Action > = new List.< Action >(); public var currentAction : Action; public var undoContainer : Transform;
// Add an action to the actions list public function AddAction ( obj : GameObject ) { var newAction : Action = new Action ( obj );
newAction.storedObject.transform.parent = undoContainer;
newAction.storedObject.transform.localPosition = Vector3.zero;
newAction.storedObject.SetActive ( false );
actions.Add ( newAction );
// Set the maximum amount of undo actions
if ( actions.Count > 10 ) {
actions.RemoveAt ( 0 );
}
currentAction = newAction;
}
// Undo the selected action public function UndoAction ( action : Action ) { action.storedObject.transform.parent = currentLevel.transform; action.storedObject.transform.position = action.storedPosition; action.storedObject.transform.localEulerAngles = action.storedRotation; action.storedObject.transform.localScale = action.storedScale; action.storedObject.SetActive ( true );
if ( action.originalObject ) {
Destroy ( action.originalObject );
}
actions.Remove ( action );
if ( actions.Count > 0 ) {
currentAction = actions[actions.Count-1];
} else {
currentAction = null;
}
}
// Undo the current action public function UndoCurrentAction () { if ( currentAction != null ) { UndoAction ( currentAction ); };
} ```
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to have animation play correctly 1 Answer
Cant Find The Variable In Another Script? 1 Answer
Help with Kongregate API? 0 Answers
How can i make my scene less laggy? 2 Answers