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 baardie1 · Jul 17, 2019 at 03:18 PM · coroutineloopspawningenemies

Wave Spawning not working

Hi, trying to use an ienumerator for a wave spawning script, i could get it to spawn enemies every few minutes but i'm trying to use waves but it just crashes when it starts

 public class SpawnController : MonoBehaviour
 {
     public GameObject Enemy = null;
     public GameObject[] enemys;
 
     public float spawnWait;
     public float startWait;
     public float waveWait;
     public float waveTimer = 5;
 
     public int waves;
     public int currentWave;
 
     
     public int waveEnemies;
     public static int enemies;
     public Transform[] spawnPoints;
 
     void Start()
     {
         
     }
     IEnumerator SpawnWavesE;
 
     void FixedUpdate()
     {
        
         if (currentWave == 1)
         {
             StartCoroutine("SpawnWaves");
         }
         if (currentWave == 2)
         {
             StartCoroutine("SpawnWaves");
         }
         if (currentWave == 3)
         {
             StartCoroutine("SpawnWaves");
         }
         if (currentWave == 4)
         {
             StartCoroutine("SpawnWaves");
         }
     }
 
     public IEnumerator SpawnWaves()
     {
         yield return new WaitForSeconds(startWait);
         while (true)
         {
 
             for (int i = 0; i < waveEnemies; i++)
             {
                 if (enemies < waveEnemies)
                 {
                     Debug.Log("Enmies:" + enemies);
                     int spawnPointIndex = Random.Range(0, spawnPoints.Length);
                     PhotonNetwork.InstantiateSceneObject(Path.Combine("Enemy", "Enemy"), spawnPoints[spawnPointIndex].position, Quaternion.identity);
                     enemies += 1;
                     yield return new WaitForSeconds(spawnWait);
                 }
 
             }
 
             if (enemies <= 0)
             {
                 Debug.Log("STARTING NEW WAVE");
                 currentWave += 1;
                 Debug.Log("Enmies:" + enemies);
                 Debug.Log("Wave: " + waves);
                 waveEnemies += 1;
                 yield return new WaitForSeconds(waveWait);
                 StopCoroutine("SpawnWaves");
             }
         }
     }

Is there something i'm doing wrong??

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
0
Best Answer

Answer by myzzie · Jul 17, 2019 at 05:19 PM

Yeah you're doing quite a lot wrong here. First off, don't start a coroutine with a string, store it in a coroutine instead like this

 Coroutine myCoroutine = StartCoroutine(SpawnWaves());

If you wish to stop the coroutine, do

 StopCoroutine(myCoroutine);

Secondly, don't stop a coroutine inside a coroutine, figure out when to yield out of it instead e.g

 While(enemies > 0)
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 baardie1 · Jul 17, 2019 at 06:51 PM 0
Share

Iv'e redone it a bit and did what you said like storing it, it works now but the only issue is it seems to be ignoring the if statment inside:

  public IEnumerator SpawnWaves()
 {

     yield return new WaitForSeconds(startWait);

     while (enemies < waveEnemies)
     {
         
         Debug.Log("Enmies:" + currentEnemies);
         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
         PhotonNetwork.InstantiateSceneObject(Path.Combine("Enemy", "Enemy"), spawnPoints[spawnPointIndex].position, Quaternion.identity);
         enemies += 1;
         currentEnemies += 1;
             yield return new WaitForSeconds(spawnWait);
         if (currentEnemies == 0)
         {
             Debug.Log("STARTING NEW WAVE");
             currentWave += 1;
             Debug.Log("Enmies:" + enemies);
             Debug.Log("Wave: " + waves);
             waveEnemies += 1;
             yield return new WaitForSeconds(1f);
             
             StopCoroutine(myCoroutine);
         }
     }

nothing happens after, the game just continues

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

119 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

Related Questions

How to start coroutine after it gets stopped 1 Answer

Best way to make melee AI damage player? 2 Answers

I want that the enemies spawn in a random radius from the player, but not on other gameobjects like a housen or a car or something. 1 Answer

Running multiple coroutines in loops with different time 2 Answers

How to change a large number of booleans as fast as possible without sacrificing framerate? 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