- Home /
Destroy a gameobject instance but also save it in a new variable
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.
Your variable is not a copy of the Game Object's data; it's a reference to the object. So if you Destroy it, the reference is null. This works for Editor undo: http://unity3d.com/support/documentation/ScriptReference/Undo.RegisterSceneUndo.html
What if you want to do a runtime undo/redo ? ... Also, even though it's a reference, it seems that when you copy the reference and destroy the original, the gameobject still remains in the scene. That's what I meant when I said the Destroy() doesn't work anymore.
Answer by ThePunisher · Jan 20, 2012 at 09:03 PM
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?
using UnityEngine;
using System.Collections;
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 ()
{
}
void OnGUI()
{
if(GUI.Button(new Rect(Screen.width/2 - 125, 0, 250, 50), "Destroy object"))
{
Destroy(obj);
}
}
}
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
That's so weird.. I wonder if this is one of those UnityScript / JS only things...
So you're using JS? I would think they'd work the same, but that's not always the case. Did you make sure that your reference your keeping is to a GameObject rather than a script?
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
What is wrong with this script? 2 Answers
Instantiated GameObject being deleted when destroying original 1 Answer
Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers
How to Play 3D Sound and Keep Script on Multiple Prefabs after Destroying a gameObject? 0 Answers