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 CerushDope · Oct 17, 2017 at 11:02 AM · gameobjectcoroutineienumeratorfindgameobjectswithtagwave

How to get if all all objects are destroyed to start another wave

I am making a simple shooter. I want the following: to keep track of current enemies. if there are no enemies I want to start the new wave. My game controller code currently looks like this: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class GameController : MonoBehaviour {
 
     public GameObject[] Bosses;
 
     public GameObject[] Enemies;
 
     public int EnemyCount = 3;
 
     public float SpawnWait = 0.1f;
 
     public float StartWait = 1f;
 
     public float WaveWait = 1f;
 
     private float waveNumber = 1;
 
     void Start () {
         StartCoroutine(SpawnWaves());
     }
 
     private IEnumerator SpawnWaves()
     {
         yield return new WaitForSeconds(StartWait);
 
         while (true)
         {
 
             Debug.Log("Starting Wave");
             if (GameObject.FindGameObjectWithTag("Enemy") != null)
                 continue;
 
             if (waveNumber % 3 == 1 && waveNumber > 1)
             {
                 Debug.Log("Sending New Enemy");
                 float spawnCoordY = Random.Range(-4, 4);
                 Vector2 spawnPosition = new Vector2(5, spawnCoordY);
 
                 System.Random random = new System.Random();
                 int enemyToSpawnIndex = random.Next(0, Bosses.Length);
 
                 GameObject currentEnemy = Instantiate(Bosses[enemyToSpawnIndex], spawnPosition,
                     Quaternion.identity);
             }
             else
             {
 
                 for (int i = 0; i < EnemyCount; i++)
                 {
                     float spawnCoordY = Random.Range(-4, 4);
                     Vector2 spawnPosition = new Vector2(5, spawnCoordY);
                     System.Random random = new System.Random();
                     int enemyToSpawnIndex = random.Next(0, Enemies.Length);
 
                     GameObject currentEnemy = Instantiate(Enemies[enemyToSpawnIndex], spawnPosition,
                         Quaternion.identity);
 
                     yield return new WaitForSeconds(SpawnWait);
                 }
 
                 EnemyCount += 2;
             }
 
             waveNumber++;
             yield return new WaitForSeconds(WaveWait);
 
         }
     }
 }

As you can see, I check if all enemies have been destroyed by seeking for enemy with tag and if no enemy is found I start next wave. For unspecified reason it has some problems. Sometimes it freezes the game (and the editor itself). I know the problem is there because when I comment out that line it works well. Please tell me what is the main problem of it ? and also if there is a better practice of doing what I want do not hesitate to also tell it to me.

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 Dragate · Oct 17, 2017 at 11:29 AM

If "Enemy" is found, you "continue;", which makes it return to "while(true)". Basically, a loop that will not finish until it finds an "Enemy" in the same frame (which causes freeze). Instead of using continue, use

 yield return new WaitForSeconds(delay); //check again after some delay

or

 yield return null; //check again in the next frame
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

104 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

Related Questions

Better solution for GameObject.FindGameObjectsWithTag 2 Answers

Execution order when Start is declared with IEnumerator return (as a Coroutine) 1 Answer

calculating player rotation direction 0 Answers

Item duration doesn't stack 3 Answers

Rotate GameObject using Coroutine 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