- Home /
Instantiate and destroy on given location
I want to have an object wich destroys on the click of a button and then instantiate an object on that same location. But the object is moving so i don't know how i should do this exactly. if it's possible to get the coordinates from a empty gameobject and instantiate it as a child of that gameobject then that would be great. Anyone who can help with this?
Answer by lampshade · Feb 15, 2011 at 12:30 PM
Attach the object that you want to be instantiated on the other objects destroy into the prefab slot. And attach this script (possibly) to the obejct that you do not want destroyed. Or if this is way off let me know.
public class DestroyOnMouseDown : MonoBehaviour {
public GameObject prefab;
private void DestroyOnClick(GameObject InstOnDestroy) {
if (OnMouseDown()) { Destroy(transform.gameObject); }
GameObject instPrefab = (GameObject) Instantiate(InstOnDestroy, prefab.transform.position, prefab.transform.rotation);
Destroy(instPrefab, 2.2f);
}
private void Update() {
if (prefab) DestroyOnClick(prefab);
//else: puporseless script
}
}
Or if this is way off let me know.
Right now it only gives me a bunch of errors about expected semicolons Tried to fix it, but i just can't xD
Your answer
Follow this Question
Related Questions
How can I replace an object while keeping the references to that object? 2 Answers
Destroy the current GameObject? 7 Answers
What should I change in this instantiating a GameObject in a script using Unity? 1 Answer
REPLACING INSTANTIATED OBJECT 0 Answers
Instantiating gameObject with custom Class properties 1 Answer