Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Sendatsu_Yoshimitsu · Nov 22, 2014 at 01:33 AM · c#coroutinefreeze

Why does this coroutine freeze my game?

I'm using a coroutine to manage NPC spawns, and it seems to my eyeballs that it runs and yields correctly, but every time I run the game it freezes up after a few seconds, and disabling this coroutine results in it no longer freezing. Do I have an obvious mistake in my implementation?

 void Awake(){
         StartCoroutine (managePedestrianPopulation());
     }
 
     IEnumerator managePedestrianPopulation(){
         while (true) {
             SpawnPedestrian(); //Check actual population density vs. desired population density, spawn new ones if under, remove old ones if way over
             yield return new WaitForSeconds(2f);
         }
     }
 
 
     void SpawnPedestrian(){
 
         if (activeSpawners.Count > 0){                                                 //If there are any active spawners in the game
             int randomIndex = Random.Range (0, activeSpawners.Count);                //Select a random spawner from the list of active ones
             Transform newPedestrian = PoolManager.Pools ["Pedestrians"].Spawn (        //Pooling system: grab a pedestrian prefab from my pool and spawn it at the spawner selected above
                 pedestrianPrefab,
                 activeSpawners[randomIndex].t.position,
                 Quaternion.identity
             );
         }
      }


Comment
Add comment · Show 2
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 Uldeim · Nov 22, 2014 at 02:11 AM 0
Share

$$anonymous$$y eyeballs agree, but they may need a checkup. :)

Have you considered that it may be the actual spawning of the pedestrians that's causing the issues? For example, if they spawn on top of each other, do you have tons of collisions? $$anonymous$$aybe try calling SpawnPedestrian a couple dozen times in Awake()?

avatar image Sendatsu_Yoshimitsu · Nov 22, 2014 at 02:46 AM 0
Share

A tertiary cause rooted in pedestrians spawning might be the problem, but in playing around a bit I'm having a heck of a time reproducing the freeze without a coroutine- pedestrians spawn one at a time, once every two seconds, and they're only fitted with one capsule collider each, so there shouldn't be enough raw physics interactions to overpower the system. I can't tell for sure, but experimentation also seems to suggest that it freezes the moment managePedestrianPopulation() gets called- I can run this with only one spawn point, and my eyes glued to it, and it will still freeze before any pedestrians pop in.

On a related note, one thing making me suspect the coroutine, and not the spawner, is that I've been able to manually spawn hundreds of pedestrians at a time without hiccups or weirdness, it wasn't until I started experimenting with ways to make it automatic that I had problems.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Addyarb · Nov 22, 2014 at 03:33 AM

It's the while loop. Change it to if (or if and else). Happened to me all the time until I realized that you can only use while loops in very specific situations.

i.e. ->

  if (true) {
              SpawnPedestrian(); //Check actual population density vs. desired population density, spawn new ones if under, remove old ones if way over
              yield return new WaitForSeconds(2f);
          }
Comment
Add comment · Show 8 · 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 Sendatsu_Yoshimitsu · Nov 22, 2014 at 03:38 AM 0
Share

Okay, that is weird. This fixes the freeze, but I can't for the life of me figure out why... would you $$anonymous$$d expanding on the technical reasons behind why a while(true){} freezes coroutines? :)

Also, is there a way to make this loop with the if? I need it to run every 2-ish seconds, but with if true, it appears to only run once.

avatar image Kiwasi · Nov 22, 2014 at 03:47 AM 1
Share

while(true) is perfectly acceptable to use in coroutines, as long as there is a yield inside the loop. Your code has this in place.

The normal cause of a freeze is an infinite loop. But its not in this code.

This code does not happen to be attached to the pedestrian prefab by any chance? If every pedestrian is running this code, then you get an infinite loop through each pedestrian spawning a new pedestrian as soon as its created. This code should be off on its own on a manager type GameObject

avatar image Sendatsu_Yoshimitsu · Nov 22, 2014 at 03:49 AM 0
Share

Nope, it's attached to a single, empty gameobject I use to hold logic controllers.

avatar image Kiwasi · Nov 22, 2014 at 03:54 AM 0
Share

That's very strange then. Your original code should have worked as written. Without seeing your project I can't really offer much more constructive help on this.

Quick question, does using the if actually produce the desired behaviour? ie do you get a random spawn every two seconds?

avatar image Bunny83 · Nov 22, 2014 at 05:29 AM 1
Share

I removed the accept state on this answer since the answer doesn't make much sense. As Bored$$anonymous$$ormon said it's perfectly fine to use a while loop like this in a coroutine.

There are a lot of other possible causes for a freeze. What about your pool manager? What does it do when it runs out of pooled objects?

Show more comments

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

28 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 avatar image avatar image avatar image

Related Questions

Can't get while loop to execute more than once in a coroutine before yielding [Solved] 2 Answers

Start Coroutine after other has finished 4 Answers

How to solve while loop lags 1 Answer

Move to point script - bad OOP and coroutines 0 Answers

Making a fill take exactly n seconds to complete 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