Search Unity

IEnumerator Help.

Discussion in 'Scripting' started by GFFG, Dec 17, 2014.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    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:
    Code (CSharp):
    1. public class Destroyable : MonoBehaviour {
    2.  
    3.  
    4.  
    5.  
    6.     public void RemoveMe() {
    7.        
    8.         IEnumerator MyMethod() {
    9.             yield return new WaitForSeconds(2);
    10.        
    11.         Debug.Log("Destroyable's RemoveMe function is being called on" + name);
    12.         Destroy (gameObject);
    13.             }
    14.  
    15.  
    16.     }
    17. }
    18.  

    Any help is greatly appreciated, thanks.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
  4. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    GFFG likes this.
  5. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    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 :D

    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.