- Home /
Destroy the first instanced prefab while still being able to generate more prefab's later
It seems that when I use Destroy(gameObject)
from a script within a gameobject prefab, and there is only one instance of that gameobject prefab onscreen, I am no longer able to instantiate the prefab, even from a different gameobject/script
How do you destroy the first without being unable to generate more?
If you instantiate an object, object being prefab, it should be in the hierarchy, so you should be able to instantiate the target object, don't do it directly, nor use the prefab in the game at the time, use the prefab in the hierarchy... If that makes sense, then play in editor, see if it gets deleted from the hierarchy, because it shouldn't...
Answer by Mike 3 · Dec 27, 2010 at 04:52 AM
Something like this, with your own methods for instantiating and destroying, but paying careful notice to the instantiated null/not null checks
private var instantiated : GameObject; var prefab : GameObject;
function Update() { if (Input.GetMouseButtonDown(0) && instantiated == null) { instantiated = Instantiate(prefab); }
else if (Input.GetMouseButtonDown(1) && instantiated != null)
{
Destroy(instantiated);
instantiated = null;
}
}