It seems like when you
Destroy(gameObjectInstance), but first save its attributes in a new variable
newvar = gameObjectInstance, the Destroy does not work anymore…
How do you destroy a gameobject instance but also save it in a new variable - something for an undo/redo system.
Well first of all, I would have thought that keeping a reference to a game object would keep the Destroy() method from being able to destroy an object, but that doesn’t seem to be the case. I tried keeping a reference to a GameObject and then destroying with Destroy() and DestroyObject() and in both cases it simply destroyed the GameObject even though I had a reference to it. Try creating an empty object (CTRL+SHIFT+N) and adding the following script to it. Then create a cube and drag it over to the public member of the script within the inspector. Run it. You should see the GameObject be destroyed. This leads me to believe that you’re reference to the object may not be valid? Are you sure your reference isn’t to a script rather than the GameObject itself?
public class GuiThing : MonoBehaviour
public GameObject obj;
private GameObject objReference;
// Use this for initialization
void Start ()
objReference = obj;
// Update is called once per frame
void Update ()
if(GUI.Button(new Rect(Screen.width/2 - 125, 0, 250, 50), "Destroy object"))
In any case, perhaps you could create a clone of the original object, destroy the original object, and disable the clone until the undo button is hit (at this point you would enable the object again). Or you could simply disable the original GameObject and enable it once it was needed again. Hope this helps