I am using the Undo system from Hidden Monk’s Gizmo asset here which i am trying to expand with delete/undelete ability in addition to undoing transforms, and by following the pattern i came up with this class:
using CommandUndoRedo;
namespace RuntimeGizmos
{
public class InstantiateCommand : ICommand
{
private GameObject objectToSpawn;
private GameObject spawnedGameObject;
public InstantiateCommand(GameObject objectToSpawn)
{
this.objectToSpawn = objectToSpawn;
}
public void Execute()
{
spawnedGameObject = GameObject.Instantiate(objectToSpawn);
}
public void UnExecute()
{
GameObject.Destroy(spawnedGameObject);
}
}
}
Then on instantiation i am doing the following which is what works with the transform commands…
InstantiateCommand instantiateCommand = new InstantiateCommand(selectedPrefab);
CommandGroup commandGroup = new CommandGroup();
commandGroup.Add(instantiateCommand);
UndoRedoManager.Insert(commandGroup);
However nothing is happening when i use Undo/Redo, and i’ve debugged the classes and can see that the object is being passed to them, am i using the system right or have i misunderstood something as to how these command systems work?
I’ve am doing that already and things seem to be moving internally, and if i call Execute() directly instead of Insert() i get 2 objects spawning so the object is there, it feels like Execute/Unexecute in my class is not being called correctly when i use UndoRedoManager.Undo().
Okay i made some progress, another part of the gizmo script was calling gizmo.ClearUndoRedoManager();.
i am now getting a “The object of type ‘Transform’ has been destroyed but you are still trying to access it” from the TransformCommand class, this is one bloody class battle.
So here’s the problem, when i delete an object and then Undo the TransformCommand class is called instead of the InstantiateCommand class, is this in order of execution issue? I am so lost in this.
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.set_position (UnityEngine.Vector3 value) (at <c4e726602a0b453fbc96db7a06c54db2>:0)
RuntimeGizmos.TransformCommand.UnExecute () (at Assets/RuntimeGizmo/Objects/Commands/TransformCommand.cs:39)
CommandUndoRedo.CommandGroup.UnExecute () (at Assets/RuntimeGizmo/UndoRedo/CommandGroup.cs:48)
CommandUndoRedo.UndoRedo.Undo () (at Assets/RuntimeGizmo/UndoRedo/UndoRedo.cs:30)
CommandUndoRedo.UndoRedoManager.Undo () (at Assets/RuntimeGizmo/UndoRedo/UndoRedoManager.cs:18)
RuntimeGizmos.TransformGizmo.HandleUndoRedo () (at Assets/RuntimeGizmo/TransformGizmo.cs:269)
RuntimeGizmos.TransformGizmo.Update () (at Assets/RuntimeGizmo/TransformGizmo.cs:165)
You’re mentioning a lot of code of which none of us have seen.
All we can really say is… you’re trying to do something with a destroyed object. Naturally, you can’t do anything with an object that’s been destroyed.
I get the coding error, the gizmo script is trying to reapply the Transform through the TransformCommand class onto the object i have deleted, when the UndoSystem should be calling the InstantiateCommand class based on the stored commands in the lists, i need help with understanding the design of this command system because apparently the commands are not going through correctly.
And if i do null check it would defeat the purpose of the command system, i shouldn’t need to do that.
Again, if you Destroy() something, it’s gone, you can’t do stuff to it. It’s GONE.
If your Undo system needs to support undoing Destroy() then you need to serialize a complete copy of what you Destroy()-ed before you Destroy() it, such that you can put it back.
Wasn’t asking for that, just wanted to see if anything stood out to experienced eyes.
I’ve got it to half-work, it’s instantiating and removing the items but only if don’t give it transform commands, when i do it seems to loose sync and throw errors, not sure how multiple commands are supposed to be handled.