destroy object after a delay?

How do i destroy object after a delay?

var destroyTime = 5;
function Update () {
Destroy(gameObject, destroyTime);
}

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);

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.

All is there http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

Hope that helps

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

destroy(gameObject,2f);