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 zeeko · Jan 21, 2015 at 04:44 PM · enemyenemy spawnwave

Spawning Waves: Second wave doesn't start?

Hey everybody, I'm almost brand new at this. My issue is, once the first wave of enemies are defeated, the second wave never begins. I'm using the first tutorial from the Unity Game Development Blueprints e-book. It's a top-down space shooter, real basic. Here's what I have (sorry if all the comments are annoying, but this is my first few days of programming ever):

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
     
     // Our enemy to spawn
     public Transform enemy;
     
     // We want to delay our code at certain times
     public float timeBeforeSpawning = 1.5f;
     public float timeBetweenEnemies = .25f;
     public float timeBeforeWaves = 2.0f;
     
     public     int enemiesPerWave = 10;
     private int currentNumberOfEnemies = 0;
     
     // Coroutine used to spawn enemies
     IEnumerator SpawnEnemies()
     {
         // Give the player time before we start the game
         yield return new WaitForSeconds(timeBeforeSpawning);
         
         // After timeBeforeSpawning has elapsed, we will enter this loop
         while(true)
         {
             // Don't spawn anything new until all of the previous
             // wave's enemies are dead
             if(currentNumberOfEnemies <= 0) 
             {
                 float randDirection;
                 float randDistance;
                 
                 //Spawn 10 enemies in a random position
                 for (int i = 0; i < enemiesPerWave; i++) 
                 {
                     // We want the enemies to be off screen
                     //(Random.Range gives us a number between the
                     //first and second parameter)
                     randDistance = Random.Range(10, 25);
                     
                     // Enemies can come from any direction
                     randDirection = Random.Range(0, 360);
                     
                     // Using the distance and direction we set the position
                     float posX = this.transform.position.x +
                         (Mathf.Cos((randDirection) * Mathf.Deg2Rad) * 
                          randDistance);
                     float posY = this.transform.position.y + 
                         (Mathf.Sin((randDirection) * Mathf.Deg2Rad) * 
                          randDistance);
                     
                     // Spawn the enemy and increment the number of enemies spawned
                     //(Instantiate makes a clone of the first parameter
                     //and places it at the second with a rotation of
                     //the third.)
                     Instantiate(enemy, new Vector3 (posX, posY, 0), 
                                 this.transform.rotation);
                     currentNumberOfEnemies++;
                     yield return new WaitForSeconds(timeBetweenEnemies);
                 }
             }
             // How much time to wait before checking if we need to 
             // spawn another wave
             yield return new WaitForSeconds(timeBeforeWaves);
         }
     }
     
     // Allows classes outside of GameController to say when we killed an enemy.
     public void KilledEnemy()
     {
         currentNumberOfEnemies--;
     }
     
     // Use this for initialization
     void Start () 
     {
         StartCoroutine(SpawnEnemies());
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }

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 Adamcbrz · Jan 21, 2015 at 04:54 PM 1
Share

Are you sure that $$anonymous$$illedEnemy is getting called?

avatar image zeeko · Jan 21, 2015 at 05:35 PM 0
Share

Hah, that was exactly the problem. Fixed! Thank you :)

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemy Wave Generator Spawn System Help! 1 Answer

Enemy not generating... 0 Answers

Next enemy wave timer issue 0 Answers

Spawn "n" Enemies Every "nth" Wave? 1 Answer

Enemy randomly disappears on startup 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