- Home /
Undo.RecordObject doesn't work
Hello,
I want to implent an undo/redo-system in a 3D virtual reality editor. I already used Undo.RegisterCreatedObjectUndo to register when a new object is created and make the action undoable. However I can not get the essential Undo.RecordObject to work, here is an example:
Undo.RecordObject(GetComponent<LaserPointer>().hitObject, "ObjectMoved");
OnTriggerDownMove(GetComponent<LaserPointer>().hitObject);
In this example I simply target an GameObject with the laserpointer (attached to the controller) and move it by holding the trigger and dragging it. Undo.RecordObject does not register any undo-operation for that.
If I use the Undo.RegisterCompleteObjectUndo function, I can use Undo.performUndo to reset the position, however it only allows to save 1 copy of the object and multiple move-steps will not be undoable step-wise.
I hope you guys can help me out, if you need additional information just tell me.
Answer by Adam-Mechtley · Feb 09, 2017 at 06:46 PM
It sounds like you are changing properties on the Transform component, but recording the state of the LaserPointer component. Make sure you pass the affected Transform component to the Undo.RecordObject()
method.
Hey, thanks for your answer. I managed to partly get the Undo.RecordObject() function to work now. I placed it inside the OnTriggerDown$$anonymous$$ove() function and used the transform of the given GameObject:
public void OnTriggerDown$$anonymous$$ove(GameObject hitObject)
{
Undo.RecordObject(hitObject.transform, "Obejct$$anonymous$$oved");
/*...rest of the function...*/
}
However it has the following problem: When I move the object from a to b and then use performUndo(), the object will be reset too position a and everything seems fine. But when I move the object from a to b and then I move the object from b to c (the object can be grabbed and moved multiple times) and then I use performUndo(), the object will always be reset to position a, even if the starting-position of the last move was somewhere else. Any ideas?
Answer by KneesWeak · Feb 12, 2017 at 08:59 AM
I managed to partly get the Undo.RecordObject() function to work now. I placed it inside the OnTriggerDownMove() function and used the transform of the given GameObject:
public void OnTriggerDownMove(GameObject hitObject)
{
Undo.RecordObject(hitObject.transform, "ObejctMoved");
/*...rest of the function...*/
}
However it has the following problem: When I move the object from a to b and then use performUndo(), the object will be reset too position a and everything seems fine. But when I move the object from a to b and then I move the object from b to c (the object can be grabbed and moved multiple times) and then I use performUndo(), the object will always be reset to position a, even if the starting-position of the last move was somewhere else. Any ideas?
Could you try calling Undo.IncrementCurrentGroup() each time the trigger button is first depressed?
Thanks a lot! This worked perfectly. So far I have some trouble really understanding the Groups of the Undo Element. Sadly the documentation doesn't really help me...
Your answer
Follow this Question
Related Questions
Undo.Record: Recording variables within array elements 0 Answers
Constant error : SaveSnapshot called without previous call to MakeSnapshot 1 Answer
Need help reassigning variables after the deletion of an object has been undone. 0 Answers
Undo in Editor only executed when I switch focus to Hierarchy 0 Answers
Custom data for undo/redo action? 0 Answers