Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Waltham · Sep 16, 2015 at 11:55 PM · c#coroutine

Code with Coroutine not working as intended.

I would like the following code to create a cube every second but it operates otherwise:it waits a second and then creates all of them without waiting a second. How can i change this code to make it create a cube, wait a second, and then create another? Thanks for your time.

     public int numCubes = 10;
 // Use this for initialization
 void Start () {
     
     for (int i = 0; i< numCubes ; i++)
     {
         for (int j = 0; j < numCubes; j++)
         {
             StartCoroutine(CreateCube (i,j));
         }
         
     }
 }
 
 IEnumerator CreateCube (int i, int j)
 {
     
     GameObject box = GameObject.CreatePrimitive (PrimitiveType.Cube);
     box.transform.position = new Vector3 (i * 2.0f, j * 2.0f, 0f);
     yield return new WaitForSeconds(1f);
     
 }
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 DoTA_KAMIKADzE · Sep 16, 2015 at 11:57 PM

Your problem is that you start many coroutines at once that will spawn 1 cube each instead of starting one coroutine that will spawn many. This is the corrected version of your code:

     public int numCubes = 10;
     void Start()
     {
         StartCoroutine(CreateCube());
     }
     IEnumerator CreateCube()
     {
         for (int i = 0; i < numCubes; i++)
         {
             for (int j = 0; j < numCubes; j++)
             {
                 GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 box.transform.position = new Vector3(i * 2.0f, j * 2.0f, 0f);
                 yield return new WaitForSeconds(1f);
             }
         }
     }
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 Waltham · Sep 17, 2015 at 01:41 PM 0
Share

Thanks a Lot. Now it seems Quite Simple and Obvious.

avatar image
0

Answer by Bunny83 · Sep 17, 2015 at 12:04 AM

Your Start method starts 100 coroutines at once. Those will all run "simultaneously" (not actually but from the a concept point of view). Waiting can only happen inside a coroutine. A coroutine runs independent from the code that started the coroutine unless you yield on that coroutine as well. However that is only possible when the starting code is also inside a coroutine.

So your Start method would need to be a coroutine as well:

 IEnumerator Start ()
 {
     for (int i = 0; i< numCubes ; i++)
     {
         for (int j = 0; j < numCubes; j++)
         {
             yield return StartCoroutine(CreateCube (i,j));
         }
     }
 }

This would work, however it creates a lot of overhead / garbage for each started coroutine. Such a simple thing would be better implemented like this:

 IEnumerator Start ()
 {
     for (int i = 0; i< numCubes ; i++)
     {
         for (int j = 0; j < numCubes; j++)
         {
             CreateCube(i, j);
             yield return new WaitForSeconds(1f);
         }
     }
 }
 void CreateCube (int i, int j)
 {
     GameObject box = GameObject.CreatePrimitive (PrimitiveType.Cube);
     box.transform.position = new Vector3 (i * 2.0f, j * 2.0f, 0f);
 }

Here you only have one coroutine (the start method) and CreateCube is a normal method.

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 Waltham · Sep 17, 2015 at 01:49 PM 0
Share

Thanks. One Question: If the Start method is an IEnumerator will it still be automatically called by the unity engine?

avatar image DoTA_KAMIKADzE Waltham · Sep 17, 2015 at 02:15 PM 0
Share

Yes.

You can see that for yourself simply by using example provided by Bunny83 or if you want to convert my example you will simply need to remove whole Start function and rename CreateCube function to Start, e.g.:

     public int numCubes = 10;
     IEnumerator Start()
     {
         for (int i = 0; i < numCubes; i++)
         {
             for (int j = 0; j < numCubes; j++)
             {
                 GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 box.transform.position = new Vector3(i * 2.0f, j * 2.0f, 0f);
                 yield return new WaitForSeconds(1f);
             }
         }
     }
avatar image Waltham · Sep 18, 2015 at 08:20 PM 0
Share

Yes, i saw that. Problem solved. Thanks. Though the concept is the same here, and that is the main thing, i prefer your original version because it seems more general and does not tie the start method to this particular problem.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Rotate an object using Quaternion.RotateTowards() inside a Coroutine? 1 Answer

How do i make sure an Object is initialized before using it ? C# 2 Answers

Can't get basic skill to work right(C#) 1 Answer


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