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 ddog · Apr 13, 2014 at 08:12 PM · coroutinewaitforseconds

Coroutine isn't working? (C#)

I want to spawn a wave of meteors, but for some reason my coroutine isn't working and the rocks all spawn at the same time. Can anyone tell me what's going on? Thanks!!

     void WaveStuff (int intensity)
     {
         for (int i=0; i<intensity; i++)
         {
             GameObject temp = Instantiate (Resources.Load("rock1"), new Vector2 (-10,10), Quaternion.identity) as GameObject;
             temp.rigidbody2D.velocity = new Vector2(10,-10);
             StartCoroutine("Wait", .5f);
         }
 
         for (int i=0; i<intensity; i=i+2)
         {
             GameObject temp = Instantiate (Resources.Load("rock2"), new Vector2 (-9,9), Quaternion.identity) as GameObject;
             temp.rigidbody2D.velocity = new Vector2(10,-10);
             StartCoroutine("Wait", 1f);
         }
 
         for (int i=0; i<intensity; i=i+5)
         {
             GameObject temp = Instantiate (Resources.Load("rock3"), new Vector2 (-8,8), Quaternion.identity) as GameObject;
             temp.rigidbody2D.velocity = new Vector2(10,-10);
             Destroy (temp, 4f);
             StartCoroutine("Wait", 2.5f);
         }
     }
 
     IEnumerator Wait(float duration)
     {
         yield return new WaitForSeconds(duration);
     }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by akauper · Apr 13, 2014 at 08:36 PM

You need to return your coroutine and wait for its execution within your main method.

To do this, change your WaveStuff method from 'void' type to 'IEnumerator' type.

Then, call StartCoroutine as, yield return StartCoroutine("Wait", 2.5f);

This is in the Documentation

I've changed your code to reflect this:

     IEnumerator WaveStuff (int intensity)
     {
        for (int i=0; i<intensity; i++)
        {
          GameObject temp = Instantiate (Resources.Load("rock1"), new Vector2 (-10,10), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          yield return StartCoroutine("Wait", .5f);
        }
  
        for (int i=0; i<intensity; i=i+2)
        {
          GameObject temp = Instantiate (Resources.Load("rock2"), new Vector2 (-9,9), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          yield return StartCoroutine("Wait", 1f);
        }
  
        for (int i=0; i<intensity; i=i+5)
        {
          GameObject temp = Instantiate (Resources.Load("rock3"), new Vector2 (-8,8), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          Destroy (temp, 4f);
          yield return StartCoroutine("Wait", 2.5f);
        }
     }
  
     IEnumerator Wait(float duration)
     {
        yield return new WaitForSeconds(duration);
     }


Comment
Add comment · Show 3 · 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 ddog · Apr 13, 2014 at 09:00 PM 0
Share

This is working now, but not exactly as I intended. I think I can figure it out now though, so thanks! :)

avatar image akauper · Apr 13, 2014 at 09:45 PM 0
Share

No problem. If any you need any more help just let me know. Coroutines can be a headache. P$$anonymous$$ me with a bit more background info about what you are trying to do and I'll be happy to help you write your script.

avatar image akauper · Apr 13, 2014 at 09:46 PM 0
Share

You are most likely looking for the InvokeRepeating method.

avatar image
0

Answer by Key_Less · Apr 13, 2014 at 08:47 PM

The yield instruction inside of the coroutine is what stops any code beyond that point from executing until the yield instruction is satisfied. Right now you start the coroutine, it yields for whatever the duration is, and then does nothing afterwards. If you want to delay something from happening, you need to add the code inside of your coroutine, for example:

 IEnumerator Wait(float duration)
 {
    // Code here will execute immediately...
    
    yield return new WaitForSeconds(duration);
 
    // Code here will execute AFTER the coroutine has yielded 
    // the amount of duration.
 }

For your issue specifically, I'd convert your WaveStuff function into a coroutine and change your StartCoroutine calls into yield instructions.

 IEnumerator WaveStuff (int intensity)
     {
        for (int i=0; i<intensity; i++)
        {
          GameObject temp = Instantiate (Resources.Load("rock1"), new Vector2 (-10,10), Quaternion.identity) as GameObject;
          temp.rigidbody2D.velocity = new Vector2(10,-10);
          yield return new WaitForSeconds(0.5f);
        }
 
        // Do the same for your other for loops...
     }

Just remember to call StartCoroutine() to start WaveStuff.

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 ddog · Apr 13, 2014 at 09:25 PM 0
Share

Thanks for the help! Things are working as intended now :)

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

23 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

Related Questions

GameObject moving back and forth, moves slightly further left each time. 1 Answer

WaitForSeconds not working in IEnumerator function using MoveNext 1 Answer

Coroutine skips WaitForSeconds 3 Answers

Coroutine wait is not working 1 Answer

Sequentially wait for multiple coroutines to finish 0 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