- Home /
WaitforSeconds woes
So Im trying to destroy enemies in a while loop, waiting 1 second in between (can they make waiting a bit harder??) Problem is, all enemies are getting destroyed at the same time, theyre not waiting for WaitForSEconds. In my while loop I call each enemy by their tag, which goes from Enemy1 to Enemy5. Heres my code.
void OnTriggerEnter(Collider otherObject)
{
int i=1;
while (i<=numenemies)
{
string tag="Enemy"+i;
destroyenemy=GameObject.FindGameObjectWithTag(tag);
Destroy(destroyenemy);
i++;
StartCoroutine(DestroyWait ());
}
}
IEnumerator DestroyWait()
{
Debug.Log ("so far...");
yield return new WaitForSeconds (1);
Debug.Log ("so good");
}
In my console from my debug log, Im getting 4 "so far..." and then 4 "so good". its not waiting for 1 sec then outputing so good. All of my enemys are destroyed immediately.
Ive been reading up on this and man its so hard to just pause the script for 1 second! what am i doing wrong?
Did you know that destroy as a built in wait function.
Destroy (gameObject, 3); // this obj will be destroyed after 3 seconds.
You can also pass in a variable of your choosing for the wait Time if you wanted more control over the time.
Answer by Dizzyman572 · Jun 06, 2014 at 08:58 PM
Why do you try something like this. You want the while loop to stop for second, correct? What I think is going on here is that you have your while loop repeatedly activating the DestroyWait function but without pause. Basically, your while loop is thinking, "Okay, I activated the Destroy Wait function. Now I'm going back to the start." The while loop isn't going to wait for the DestroyWait function to end. So what you want to do is move the while loop into the DestroyWait function and activate the DestroyWait Function in the OnTriggerEnter function. Plug the code in below and see if it works.
void OnTriggerEnter(Collider otherObject) {
StartCoroutine(DestroyWait ());
}
IEnumerator DestroyWait()
{
int i=1;
while (i<=numenemies)
{
string tag="Enemy"+i;
destroyenemy=GameObject.FindGameObjectWithTag(tag);
Destroy(destroyenemy);
i++;
yield return new WaitForSeconds(1);
}
}
Answer by LumpySpoon · Jun 06, 2014 at 08:53 PM
Instead of `StartCoroutine(DestroyWait ()); try StartCoroutine("DestroyWait");
Just for reference here is the unity tutorial on Coroutines
Answer by Gatu · Jun 06, 2014 at 09:58 PM
Inside OnTriggerEnter() you start the Coroutine but you do not wait for it to end before continuing your while loop. So the whole while loop executes immediately(practically like StartCoroutine(DestroyWait ()); was missing).
In order to wait for the Coroutine to end, before moving to your next 'while' iteration, you must make OnTriggerEnter a Coroutine too. You do this by changing the return type from void to IEnumerator.
Finally to wait for the Coroutine you use: yield return StartCoroutine(DestroyWait ()); I guess you already knew this part cause you already did it inside DestroyWait().
Oh my goodness, that is awesome! I didn't you could do that! :D