Alternate way of handling coroutine wait times

Instead of passing wait times around, I set it up to wait for the coroutine to end:

private IEnumerator CheckForDeathRoutine()
    {
        yield return StartCoroutine(flash.FlashRoutine());
        CheckForDeath();
    }
4 Likes

Great Idea.

Works great

I did the same, but by changing DetectDeath() itself into a coroutine:

    IEnumerator DetectDeath()
    {
        yield return flash.FlashRoutine();
        if(currentHealth <= 0)
        {
            Destroy(gameObject);
        }
    }

I just added an overload parameter to Destroy(gameobject).

Destroy(gameobject, delay);

It’ll take a float already, and you can capture the knockback time or anything else.

Good to see lots of different ways to do stuff. Thanks for sharing yours too. :slight_smile:

2 Likes

I used a while loop in my Die Routine

        private IEnumerator DieRoutine()
        {
            if (_isDying) yield break;
            _isDying = true;

            if (!_hasFlash)
            {
                Die();
                yield break;
            }

            while (_flash.IsFlashing)
            {
                yield return null;
            }

            yield return null;

            Die();
        }

In Flash.cs

        private IEnumerator FlashRoutine()
        {
            if (flashMaterial == null) yield break;
            if (_isFlashing)
            {
                _spriteRenderer.material = _defaultMat;
                yield return null;
            }
            else
                _defaultMat = _spriteRenderer.material;

            _isFlashing = true;
            _spriteRenderer.material = flashMaterial;
            yield return new WaitForSeconds(restoreDefaultMatTime);
            // ReSharper disable once Unity.InefficientPropertyAccess
            _spriteRenderer.material = _defaultMat;
            _isFlashing = false;
        }
1 Like

Privacy & Terms