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 /
  • Help Room /
avatar image
0
Question by roger4325 · Aug 04, 2018 at 04:14 AM · error message

Start Coroutine works once but not a second time

So i'm trying to add a boss wave into the Space shooter game unity has on its Learn page. I'm trying to make it so that on wave 10 you spawn a boss and after the boss is dead you return to the regular waves. Start Co-routine works for them but not for mine and I have used it plenty of other times successfully.

private void Update() {

     if (restart)
     {
         if (Input.GetKeyDown(KeyCode.R))
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
         }
     }

     if (wave == 10)
     {
         StartCoroutine(BossWave());
     }

     if (Input.GetKeyDown(KeyCode.Q))
     {
         powerUp = true;
     }

     if (powerUp)
     {

         powerUpText.text = "Power Up : Press E";
         if (Input.GetKeyDown(KeyCode.E))
         {
             playerController.fireRate = playerController.fireRate * 0.1f;
             StartCoroutine(PowerUpWait());
             powerUpLock = 1;
         }
     }
 }


IEnumerator SpawnWaves () {

     while (game == true)
     {
         yield return new WaitForSeconds(startWait);
         while (round == true)
         {
             Debug.Log("round == true");
             for (int i = 0; i < hazardCount; i++)
             {
                 GameObject hazard = hazards[Random.Range(0, hazards.Length)];
                 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate(hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds(spawnWait);
             }
             yield return new WaitForSeconds(waveWait);
             wave += 1;
             testText.text = "Wave: " + wave;
             if (gameOver)
             {
                 restartText.text = "Press 'R' for Restart";
                 restart = true;
                 break;
             }
         

         }
     
     }
  }

 IEnumerator BossWave ()
 {
     Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
     Quaternion spawnRotation = Quaternion.identity;
     Instantiate(boss, spawnPosition, spawnRotation);
     round = false;

     if (bossHealth.health <= 0)
     {
         round = true;
     }

If you guys could throw me a bone that would be amazing! Thank you in advance for any advice you guys give me. You are awesome!

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

Answer by gashraf · Aug 05, 2018 at 01:20 PM

Hi @roger4325,

If I read your code correctly, the SpawnWaves keeps spawning new regular waves, except on wave#10 where the boss wave spawns the boss and turns the variable "round" to false. The variable "round" needs to be turned back to true once the boss is dead. Where I see an issue here is that you are not waiting for the boss to die... the if check in your BossWave routine is being checked only once, immediately after the birth of the boss... thereafter, you are exiting the coroutine, never to execute this check again! So here is what you can do, you can wait till the boss dies before exiting the coroutine, as follows:

 IEnumerator BossWave()
 {
      ...
 
      round = false;
 
 /* earlier code
      if (bossHealth.health <= 0)
      {
          round = true;
      }
 */
 
     //new code
     //keep waiting and yielding control till boss gets killed
      while(bossHealth.health > 0)
           yield return null;
 
     //if you have reached here, means the boss is dead, yeah!
      round = true;
 
 }//end of coroutine
Comment
Add comment · 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
0

Answer by roger4325 · Aug 16, 2018 at 10:49 PM

This helped so much thank you!

Comment
Add comment · 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

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

153 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 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 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 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 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 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

[solved] Collision damage and Health Controller dont work 2 Answers

Can't decompress package Unity 2017.1.f3 1 Answer

Hi, Unexpected symbol at end of file. 0 Answers

StartCoroutine error message in C# 1 Answer

ArgumentOutOfRangeException 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