x


destroy object after a delay?

How do i destroy object after a delay?

more ▼

asked Feb 01, 2012 at 07:04 AM

avatar image

JBoy
222 96 93 96

Thanks All!

Feb 01, 2012 at 11:13 PM JBoy

@JBoy: don't post comments as answers. Answers should exclusively be used to answer the question. Read the FAQs for more information.

I've converted your answer into a comment.

Feb 01, 2012 at 11:16 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Feb 01, 2012 at 07:07 AM

avatar image

BiG
5.7k 13 23 69

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 01, 2012 at 07:08 AM

avatar image

MP2fps
126 7 6 6

This.

The Destroy(); method has the functionality you are looking for integrated already. Just use the above overload and you're good to go.

Feb 01, 2012 at 08:52 AM asafsitner

I should have realized this through the Scripting API. I'd thumbs up but can't yet. Thanks.

Jan 14 at 12:25 PM Guppie1337
(comments are locked)
10|3000 characters needed characters left

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


more ▼

answered Dec 05, 2013 at 07:05 PM

avatar image

wrobel221
51 3

Thanks! Very useful when you want to destroy a gameobject outside of a MonoBehaviour script, with no coroutine access. And the code is cleaner even when you do have coroutine access.

Mar 12 at 08:01 AM petrucio
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Feb 01, 2012 at 07:06 AM

avatar image

fafase
29.5k 67 83 146

(comments are locked)
10|3000 characters needed characters left
 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

more ▼

answered Nov 22, 2012 at 08:49 PM

avatar image

Wiebren de Haan
38 11 11 14

will you provide this in c#

Jul 30, 2014 at 03:26 PM Coffee with Venky

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

Mar 16 at 05:40 PM Calum1015
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1946
x1364
x323
x109
x69

asked: Feb 01, 2012 at 07:04 AM

Seen: 17719 times

Last Updated: Mar 16 at 05:40 PM