# IEnumerator Help.

**URL:** https://discussions.unity.com/t/ienumerator-help/560586
**Category:** Scripting
**Tags:** Scripting
**Created:** [December 17, 2014, 10:40am UTC](https://discussions.unity.com/t/ienumerator-help/560586 "2014-12-17T10:40:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![GFFG](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gffg/32/198424_2.png) [@GFFG](https://discussions.unity.com/u/GFFG)
#### Post date: [December 17, 2014, 10:40am UTC](https://discussions.unity.com/t/ienumerator-help/560586/1 "2014-12-17T10:40:38Z")

</div>

I don’t know any programming but I’m trying to understand. Learning by doing and all that.

I’ve got a click action script that calls another script with a RemoveMe() function. I want to use IEnumerator to delay the dissapearence of the object with that RemoveMe() script on it. I’ve tried putting the following IEnumerator lines everywhere but I can’t figure out how to write it to make it work. This doesn’t work but it’s what I’ve got so far:

```csharp
public class Destroyable : MonoBehaviour {

    public void RemoveMe() {
       
        IEnumerator MyMethod() {
            yield return new WaitForSeconds(2);
       
        Debug.Log("Destroyable's RemoveMe function is being called on" + name);
        Destroy (gameObject);
            }

    }
}

```

Any help is greatly appreciated, thanks.

---

<div class="post-metadata">

### Author: ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)
#### Post date: [December 17, 2014, 10:49am UTC](https://discussions.unity.com/t/ienumerator-help/560586/2 "2014-12-17T10:49:52Z")

</div>

[http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines](http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines)

---

<div class="post-metadata">

### Author: ![A.Killingbeck](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@A.Killingbeck](https://discussions.unity.com/u/A.Killingbeck)
#### Post date: [December 17, 2014, 11:06am UTC](https://discussions.unity.com/t/ienumerator-help/560586/3 "2014-12-17T11:06:14Z")

</div>

I think it would be better if he started off with [Functions - The complete C# tutorial](http://csharp.net-tutorials.com/basics/functions/) and  
[Variable and Method Scope in Microsoft .NET | Microsoft Learn](http://msdn.microsoft.com/en-us/library/ms973875.aspx)

---

<div class="post-metadata">

### Author: ![Zaladur](https://avatars.discourse-cdn.com/v4/letter/z/22d042/32.png) [@Zaladur](https://discussions.unity.com/u/Zaladur)
#### Post date: [December 17, 2014, 2:49pm UTC](https://discussions.unity.com/t/ienumerator-help/560586/4 "2014-12-17T14:49:40Z")

</div>

While you definitely should read those links above, Destroy has a delay function build in…

> **[Unity - Scripting API: Object.Destroy](https://docs.unity3d.com/ScriptReference/Object.Destroy.html)**

---

<div class="post-metadata">

### Author: ![GFFG](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gffg/32/198424_2.png) [@GFFG](https://discussions.unity.com/u/GFFG)
#### Post date: [December 18, 2014, 6:11am UTC](https://discussions.unity.com/t/ienumerator-help/560586/5 "2014-12-18T06:11:06Z")

</div>

Thanks for the links, As Zaladur said Destroy has a delay function apparently.

All I had to do for this issue was remove the IEnumerator code and write [Destroy](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Destroy)(gameObject, 3); to delay destroy 3 seconds 😄

But I appreciate the links, I will have to learn about IEnumerator and Coroutines at some point so I’m glad to have these links.
