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 Cunnah · Jun 07, 2013 at 08:51 AM · c#coroutinefor loopforloop

Getting For Loops to work in a Coroutine

Hello,

I have created a function that has two for loops inside for altering a terrain. The function itself works fine but it makes the game hang while it is processing. To fix this I have changed the function to be a co-routine with a yield in the for loop so it returns to function update after each pass.

After several Debug messages placed about the code I have determined that the co-routine is being called by StartCoroutine() but the for loop isn't. Anyone know why that would be?

Here is a stripped down version of the code.

     void Update ()
     {
 
         if (generate)
             
             StartCoroutine(GenerateLandmass ());
             
             Debug.Log("Setting Seed");
             
             generate = false;
         }
         if (newTerrainInfo)
         {
             Terrain.activeTerrain.terrainData.SetHeights(0,0,heightMap);
             newTerrainInfo = false;
         }
     }
     
 
 
 
     IEnumerator GenerateLandmass ()
     {
         //this shows in the console
         Debug.Log("Started world gen co-routine");
         
         for (int y = 0; y < worldSize; y++)
         {
                         //this doesn't show
             Debug.Log("row");
             for (int x = 0; x < worldSize; x++)
             {
                 DoStuff();
                 yield return 0;
             }
         }
         newTerrainInfo = true;
         
         
     }
Comment
Add comment · Show 4
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 Jamora · Jun 07, 2013 at 09:04 AM 1
Share

where and how is worldSize set?

avatar image BiG · Jun 07, 2013 at 09:24 AM 1
Share

@Cunnah, there's something strange in your code. The test in line 4 doesn't have its open bracket (this lets the message in line 8 to be always displayed, regardless of the previous instruction). However, I don't understand how it can be compiled, with the closed bracket at line 11. Is there a copy&paste mismatch, maybe?

avatar image moghes · Jun 07, 2013 at 09:48 AM 0
Share

How did you know that the for loop is not being called? I assumed that you are reading "row" once in your console.. Is it the case?

avatar image Cunnah · Jun 07, 2013 at 11:01 AM 0
Share

No the row line never appears, only the first Debug message. I concluded that the for loop never goes through even one iteration. Also the code is copied from a much larger section so it might be a little butchered (The current code is not exactly elegant at the moment).

I should add the program doesn't hang it calls the co-routine and then does nothing after that.

EDIT

@Jamora you might have a point, I originally called my function (DoStuff()) via another... I think worldsize = 0; and is never set in which case my code is working perfectly and I am an idiot.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Terminal-like GUI, wait for input 1 Answer

How can I create variables for each materials to show in inspector (Using Editor) ? 0 Answers

Coroutine without MonoBehaviour 6 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