base.Update() makes coroutine stop

Hi! I have a base class for my physical objects. It has an update method like in the code below.

public class PhysicsObjectBase : MonoBehaviour
{
    public virtual void Update()
    {
        if (transform.position.y < GameManager.Instance.DestroyObjectsThreshold)
        {
            Destroy(gameObject);
        }
    }
}

I have a child class for this which does its own logic and then calls base.Update(). This class also has a coroutine which is called when the object has 0 HP left.

public class DestructibleBase : PhysicsObjectBase
{
    public override void Update()
    {       
        if (DrawExplosionPosition)
        {
            DrawExplodePosition();
        }

        base.Update();
    }

    ...

    protected IEnumerator DestroyChunksAndGameObject()
    {
        yield return new WaitForSeconds(DestroyAfterExplosionTime);

        var alpha = 1.0f;

        foreach (var chunk in _chunks)
        {
            // do some stuff
        }

        ...

        foreach (var chunk in _chunks)
        {
            Destroy(chunk.gameObject);
        }

        Destroy(gameObject);

        yield return null;

}

However, when calling base.Update() the coroutine will never continue after yield return new WaitForSeconds(DestroyAfterExplosionTime);. It will execute that line though, but never resume execution.

If I remove the call to base.Update() everything works as expected again. I cannot figure out why the coroutine stops executing, anyone have any idea?

Have a nice day!

If the call to base.Update causes the Destroy(gameObject) line to fire, then that’ll stop the coroutine. Coroutines stop running as you destroy the object their MonoBehaviour is attached to.

3 Likes

Ah, I see what the problem is now. I was sure it wasn’t called for this object but it actually is. Thanks for pointing me in the right direction!

1 Like