WaitForSeconds seems to wait forever in C#

I’m doing a little project and I want to make a target respawn some seconds after dies. I’m using a coroutine but nothing happens after the ‘yield return new WaitForSeconds(3);’
I am calling it with StartCoroutine() and I don’t recieve any errors, it just doesn’t work. I do see the ‘Boop’ on the console but I don’t see the ‘BOOPBOOPBOOP’

using UnityEngine;
using System.Collections;

public class bulletdetect : MonoBehaviour {

    private float Health = 10f;


    void OnTriggerEnter (Collider col) {
        if (col.CompareTag ("Bullet")) {
            Health -= 2;
        }           
    }
    void Update() {
        if (Health <= 0f) {
            Health = 10f;
            Destroy (gameObject);
            StartCoroutine ("respawn");
        }           
    }

    IEnumerator respawn() {       
        Debug.Log ("Boop"); yield return new WaitForSeconds (3f);
        Debug.Log ("BOOPBOOPBOOP");
        Instantiate (gameObject);
    }
}

That’s just happening because you’re destroying the object when you start the coroutine. When the objects gets destroyed (probably in the next frame) the coroutine will also get destroyed so will never finish. Instead of destroying the object you’d be better off hiding it until the coroutine is finished and then destroying it.

1 Like

Thanks!

Super Thanks!!!