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
0
Question by jaydentajwilson · Sep 01, 2021 at 01:43 PM · loopspawningloopingenemy spawnwaves

NEED HELP! Wave spawner just wont work!,NEED HELP! Wave spawner not working :(

I'm trying to make a wave spawners that summons enemies. Once all enemies from that wave are killed, the next one should start. No matter how hard I try, It'll only go straight to the next wave once one has finished or once i've killed everything, the next wave doesnt even start. Here is what i've got:

using System.Collections; using UnityEngine; using TMPro;

public class WaveSpawner : MonoBehaviour { int waveCount = 1;

 public float spawnRate = 1.0f;
 public float timeBetweenWaves = 3.0f;

 public int enemyCount;

 public GameObject enemy;

 public bool waveIsDone = true;
 public Transform SpawnPoint;
 public GameObject[] EnemyRemains;

 void Update()
 {
     EnemyRemains = GameObject.FindGameObjectsWithTag("Enemy");
     if (waveIsDone == true)
     {
            StartCoroutine(waveSpawner());
     }     
 }

 IEnumerator waveSpawner()
 {
     waveIsDone = false;

     for (int i = 0; i < enemyCount; i++)
     {
         GameObject enemyClone = Instantiate(enemy, SpawnPoint);
         yield return new WaitForSeconds(spawnRate);
     }

     spawnRate -= 0.1f;
     enemyCount += 3;
     waveCount += 1;

     if (EnemyRemains.Length == 0)
     {
         yield return new WaitForSeconds(timeBetweenWaves);
         waveIsDone = true;
         Debug.Log("ITWORKS");
     }

 }

}

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 New_Game_Ideas · Sep 05, 2021 at 03:22 PM 0
Share

I am not sure but it is likely because of the position of the StartCoroutine command. You cannot use StartCoroutine normally in Update. You need to use call your IEumarator in Start function and use Start in Update.

For example: void Start() { StartCoroutine(waveSpawner()); }

  void Update()
  {
     Start();  
  }

Note: It is not a full script but I think you can pace them to work.

avatar image rage_co New_Game_Ideas · Sep 05, 2021 at 05:10 PM 0
Share

im not too sure about this, coroutines can be called quite fine with Update directly plus i would advise against calling Start() from anywhere. I think the real problem is that waveIsDone is only set true if the coroutine is called which is called when it is true and there are no enemies, aka after any enemy is instantiated, it'll never be called...im in a bit of a hurry rn, so ill look into this when i wake up tomorrow hehe, please excuse me @jaydentajwilson

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sparka_ha · Sep 06, 2021 at 05:25 PM

The problem with your wave spawner is that you only check once at the end of the spawning co-routine if all remaining enemies were killed (directly after increasing the waveCount in waveSpawner().

If you would check the length of EnemyRemains in your update loop, you should see your expected spawning behaviour.

Comment
Add comment · Show 1 · 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 sparka_ha · Sep 06, 2021 at 05:29 PM 0
Share

Also I would recommend not to use 'GameObject.FindGameObjectsWithTag("Enemy");' in your Update loop since it usually is considered an operation that is too costly to do it every frame (which is the rate at which Update() is called).

Instead I would recommend using a more 'event-based' approach where killed enemies notify the WaveSpawner about their death.

If that is too complicated, you could also use a separate co-routine checking for the end of the wave that is called less frequent (twice a second or less frequent may be totally fine for your use case)

avatar image
0

Answer by Bunny83 · Sep 07, 2021 at 10:40 AM

Like sparka_ha said, you have several issues in your code. First of all, I would highly recommend to not start a new coroutine each wave. Just keep the coroutine running. Something like this should work

 // [ ... ]
 public List<GameObject> EnemyRemains;
 void Start()
 {
     StartCoroutine(waveSpawner());
 }
 IEnumerator waveSpawner()
 {
     while (true)
     {
         for (int i = 0; i < enemyCount; i++)
         {
             GameObject enemyClone = Instantiate(enemy, SpawnPoint);
             EnemyRemains.Add(enemyClone);
             yield return new WaitForSeconds(spawnRate);
         }
         spawnRate -= 0.1f;
         enemyCount += 3;
         waveCount ++;
         // wait until all enemies have been destroyed.
         while (EnemyRemains.Length > 0)
         {
             EnemyRemains.RemoveAll(o=>o==null || !o.activeSelf);
             yield return null;
         }
         yield return new WaitForSeconds(timeBetweenWaves);
     }
 }

Note that I changed the "EnemyRemains" variable from an array to a List so we can actually add the created enemies to the list. Inside the waiting loop after everything has been spawned we just remove all elements which have been destroyed (or that have been deactivated in case you use a pool). So when the list is empty, all elements have been destroyed / defeated.


There are a few things that may be problematic. You subtact 0.1 from the "spawnrate" (which is actually a spawn delay, not a rate) but your spawn rate starts at 1 second. So after 10 waves your "spawn rate" would be 0. It's more common to reduce such attributes with a percentage by multiplying the value by something like "0.98f" which would reduce the delay by 2% of the current value. So it gets faster and faster but only approaches a delay time of 0 at infinity.

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

130 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

Related Questions

Is there a way to loop mp3 files seamlessly? 2 Answers

Nested Array? Populating a menu using a loop. 0 Answers

Spawner control and ending the level 0 Answers

Instantiating at different rates 1 Answer

How to get first script from gameobject while having propery : Unity 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