IEnumerator Help.

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:

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.

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

1 Like

I think it would be better if he started off with Functions - The complete C# tutorial and
Variable and Method Scope in Microsoft .NET | Microsoft Learn

1 Like

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

1 Like

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(gameObject, 3); to delay destroy 3 seconds :smile:

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.