Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

yield WaitForSeconds(1); Never Destroys GameObject

Discussion in 'Scripting' started by VICTOM, May 9, 2007.

  1. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    What's causing this?
    If I comment out the yield WaitForSeconds(1); it works.
    If I try to yield WaitForSeconds(1); it never destroys the object. What gives?

    Code (csharp):
    1. function CuttleFishEatsCandy(collision : Collision){
    2.     collision.gameObject.GetComponent(S_CuttleFish).swimming = false;
    3.     yield WaitForSeconds(1);
    4.     collision.gameObject.Destroy(collision.gameObject);
    5. }
    If I put a print statement in after the yield it prints. Is the collision information no longer valid?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The most likely thing i can think of is that the game object on which the coroutine runs gets destroyed by someone before 1 second passes.

    Have you tried printing the name of the game object you are about to destroy before destroying it.
     
  3. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    That sounds very likely. Thanks.

    Cheers,