Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by prof3ta · Jun 06, 2014 at 08:15 PM · c#waitforsecondsienumerator

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oasisunknown · Jun 06, 2014 at 08:41 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
             
             
         }
         
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by LumpySpoon · Jun 06, 2014 at 08:53 PM

Instead of `StartCoroutine(DestroyWait ()); try StartCoroutine("DestroyWait");

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oasisunknown · Jun 06, 2014 at 08:57 PM 0
Share

Just for reference here is the unity tutorial on Coroutines

Unity Learn Coroutines

avatar image
0

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().

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dizzyman572 · Jun 06, 2014 at 10:42 PM 0
Share

Oh my goodness, that is awesome! I didn't you could do that! :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't get past WaitForSeconds in my coroutine 1 Answer

c# - Having problems with the WaitForSeconds() method. 1 Answer

Pause script until animation is finished. 1 Answer

c# waitforsecconds 2 Answers

Loop with WaitForSeconds in IEnumerator appears to be incorrect by 10-15% 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges