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 RealMTG · Nov 21, 2014 at 03:26 PM · coroutinewhilestops

Coroutine stops when changing bool

Hi!

I have a coroutine that spawns my enemies and to make so it doesn't spawn like 500 of them, I have a simple check for that. This is the check code.

 GameObject[] allZombies = GameObject.FindGameObjectsWithTag("Enemy");
         if(allZombies.Length >= maxZombies){
             spawning = false;
         } else {
             spawning = true;
         }

It basically checks for the amount of zombies and if it is above max zombies, it sets the spawning bool to false and when there aren't enough zombies it sets it to true. But when this happens the coroutine stops. It uses a basic while(spawning == true) to spawn stuff. What can I do for this to keep going?

Thanks

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

1 Reply

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

Answer by Slev · Nov 21, 2014 at 03:32 PM

The Coroutine stops because it has reached the end of its function

 while (spawning == true) {
   //do stuff
 }
 //leave function

Instead what you want to do is:

 while (true) { //or some other control value
   if (spawning) {
     //stuff
   }
   else {
     //different stuff
   }
 }

Coroutines can run infitely as long as you use yield returns.

Comment
Add comment · Show 5 · 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 RealMTG · Nov 21, 2014 at 03:52 PM 0
Share

I tested this out. I changed

 while(spawning == true){
             GameObject spawnpoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
             Instantiate(normalZombie, spawnpoint.transform.position, Quaternion.identity);
             yield return new WaitForSeconds(normalZombieDelay);
         }

to

 while(true){
             if(spawning == true){
                 GameObject spawnpoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
                 Instantiate(normalZombie, spawnpoint.transform.position, Quaternion.identity);
                 yield return new WaitForSeconds(normalZombieDelay);
             }
         }

When I tried to play the game it crashed the editor.

avatar image Slev · Nov 21, 2014 at 03:54 PM 0
Share

@Real$$anonymous$$TG Yeah you need to have a yield return outside of the if, right now if that if fails your coroutine keeps cycling trying to find a yield to return to the iterator.

avatar image RealMTG · Nov 21, 2014 at 03:59 PM 0
Share

@Slev I'm sorry but I have not done anything like this before so I need some help. I have added a return true; under the if statement but that doesn't do anything. It spawns one enemy and then stops. I also tried with return false;. What should I put?

EDIT: Just noticed you said yield return. I did try and put yield return; but that just gives me an error. So, what can I put?

avatar image Slev · Nov 21, 2014 at 04:03 PM 0
Share

@Real$$anonymous$$TG I would do it like this:

 while(true){
   if(spawning == true){
     GameObject spawnpoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
     Instantiate(normalZombie, spawnpoint.transform.position, Quaternion.identity);
     yield return new WaitForSeconds(normalZombieDelay);
   }
   else {
     yield return null; //or some variation with a wait
   }
 }
avatar image RealMTG · Nov 21, 2014 at 04:05 PM 0
Share

@Slev Thanks! You solved my 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

26 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

Related Questions

StopCoroutine() is not stopping my coroutines 1 Answer

C# while within foreach doesn't complete 2 Answers

How to wait for Coroutine to finish? 2 Answers

Please help with While Loops 1 Answer

How to solve while loop lags 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