- Home /
destroy object after a delay?
How do i destroy object after a delay?
Yes, but you should really ask that yourself or search before just commenting on an old post. By doing that you are "resurrecting" an old question that is already 5 YEARS old, and making a new question that has been asked soon be replaced by it. You see, i just had to post again just to warn you...
Answer by BiG · Feb 01, 2012 at 07:07 AM
It's easy:
Define a function like this:
var delay = 2.0; //This implies a delay of 2 seconds.
function WaitAndDestroy(){
yield WaitForSeconds(delay);
Destroy (gameObject);
}
Call this function when you want to destroy it.
Answer by MP2fps · Feb 01, 2012 at 07:08 AM
var destroyTime = 5; function Update () { Destroy(gameObject, destroyTime); }
This.
The Destroy();
method has the functionality you are looking for integrated already. Just use the above overload and you're good to go.
I should have realized this through the Scripting API. I'd thumbs up but can't yet. Thanks.
Answer by wrobel221 · Dec 05, 2013 at 07:05 PM
You can just set time delay in Destroy method
Object.Destroy(gameObject, 2.0f);
Here's the method description
static void Destroy(Object obj, float t = 0.0F);
Thanks! Very useful when you want to destroy a gameobject outside of a $$anonymous$$onoBehaviour script, with no coroutine access. And the code is cleaner even when you do have coroutine access.
Answer by fafase · Feb 01, 2012 at 07:06 AM
All is there http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
Hope that helps
Answer by Wiebren-de-Haan · Nov 22, 2012 at 08:49 PM
var Seconds = 10;
function Update(){
Destroy();
}
function Destroy(){
yield WaitForSeconds(Seconds);
Destroy(gameObject);
}
Add this to a javascript, set the time in Seconds (Use the var). And the gameobject will be destroyed after in this script 10 seconds
I know this is a late comment @Coffee with Venky, but all you would have to do in order to convert it would be to change the "function's" with "void's" and "var Seconds = 10" to "public float Seconds;"