Hi, I’m actually trying to remove enemies from my scene few seconds after they died.
It’s working fine without delay. But when I try to use WaitForSeconds function nothing happend. Can someone tell me what is wrong with my script please? 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Invector;
public class SkeletonManager : MonoBehaviour {
private vCharacter iChar;
// Use this for initialization
IEnumerator Start()
{
yield return new WaitForEndOfFrame();
iChar = GetComponent<vCharacter>();
if (iChar != null)
{
Debug.Log("Listen onDead event");
iChar.onDead.AddListener(delegate { StartCoroutine(OnDead()); });
}
}
// Update is called once per frame
public IEnumerator OnDead() {
Debug.Log("Destroying dead skeleton.");
yield return new WaitForSeconds(1f);
Debug.Log("Waited for 1 second.");
Destroy(gameObject);
yield break;
}
}
Is vCharacter also destroying gameObject? Or making it inactive? It appears that both SkeletonManager and vCharacter are both components of the same gameObject, so I’m wondering if the code responsible for invoking onDead is also destroying the gameObject, and therefore killing all coroutines attached to that gameObject before they finish.
Generally speaking, if a coroutine doesn’t finish, it’s because its parent either got destroyed or was made inactive.
1 Like
Omg, It does’nt actually destroy the gameObject but the components attached to it. So it destroy my SkeletonManager and that explain why it works without waiting a second and not when we wait for seconds ?
So there is no error in this file ? I was sure I made somthing wrong on this script and didn’t think about other scripts ><
Anyway the vCharacter is from a plugin so I’ll have to check on the forum of the plugin if I can bypass this feature.
Thanks 
Alternatively, you could just create a new gameObject, attach it as a child to SkeletonManager’s gameObject, and put the vCharacter component on that. That way, your coroutine (started by SkeletonManager) will be safe from vCharacter.
Too much power in only 2 posts!

Thanks again haha.
1 Like