- Home /
How to insert an object after Destroy(gameObject)
Hello, I was wondering for quite some time how to, after the destroy function is called, insert another game object. I've tried the Instantiate function but it doesn't seem to work. Here is the script that I'm using:
function OnTriggerEnter(hit : Collider)
{
Destroy(gameObject);
}
So I was just wondering if it is at all possible to create another game object right after this function... and I would also like to know how to make the other object appear after the destroy is called, but in another script.
Okay thanks! Do you know of a function that could do that? The only one I know is Instantiate but I've tried again and again but I don't know how to work it. The documentation is confusing...
Answer by fafase · Mar 18, 2012 at 04:06 PM
var prefabOfYourObject : Transform; // here you drag the prefab in the inspector
var newObject = Instantiate(prefabOfYourObject, transform.position,Quaternion.identity);
var newObject means you want to declare a new variable and this variable will be given the information coming in Instantiate(1,2,3);
1= the prefab of the object you want to create
2= where you want to appear, in this case transform.position it is taking the position of the object the script is attached to. You can give any value as long as it is a Vector3(x,y,z).
3= The rotation of the object when created, Quaternion.identity means you want it to appear as the prefab is (more or less), Quaternion is pretty complicated and deals with complex number so.... Here you could give any kind of other rotation there, if you want your guy upside down or else.
The good idea would obviously to be that your prefab has a script attached to it so that your new object is not just there counting clouds.
Okay, that seemed to work after I fixed a bunch of errors, but there's still a small problem. After using the script you provided (which I altered a bit), which was
var object : Transform;
function OnTriggerEnter(hit : Collider){
Instantiate (object, transform.position, transform.rotation); }
It makes the instantiated object exactly where the object you touch is. I've tried many things but I cannot get it to appear anywhere else but where that object is...
As I said transform.position is the position of the object the script is attached to (the trigger collider). If the collision happens at (10,1,10) then your new object will appear at (10,1,10) if you want it to appear somewhere else just use var newObject = Instantiate(prefab, Vector3(x,y,z),Quaternion.identity);
"(before you even touch it)" that is not possible to happen before you enter the collider so you should check the size of both colliders.
Oh I'm sorry. I didn't understand at first. It worked! Thank you so much!
Answer by Kleptomaniac · Mar 18, 2012 at 03:24 AM
You can't use any other calls after the destruction of the gameObject, because the script will also be destroyed along with the gameObject. If you want to do something after you have destroyed a gameObject, it's as simple as not attaching the script to the object you want to destroy, but to an empty gameObject. :)
Hope that was clear enough, Klep
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Checking Instantiate/Destroy has been called | Checking number of scene GameObjects 1 Answer
Having problem with Destroying GameObject 0 Answers
Destroying 2D Array of Instantated Objects 2 Answers
Accessing children of an instance doesn't work every time 1 Answer