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 game0bject · Sep 05, 2014 at 11:02 AM · enemytimetower-defensewave

Next enemy wave timer issue

Hi,

I'm creating a tower defence game.

I'm having trying to spawn a second batch of enemies (next wave) after the first batch has spawned (when enemiesSpawned >= enemiesToSpawn) after a certain amount of time has passed. so far the first batch will spawn and the second batch will not.

can anyone please help me figure this problem out?

Thanks,

 #pragma strict
 var enemyWayPoints:Transform[];
 var enemySpawn:Transform;
 var enemy:GameObject;
 var enemies:Array = new Array();
 
 
 var waveTime = 1;
 private var nextWaveTime = 0.0;
 
 var currentWave:int;
 var enemyCount:int;
 var enemiesToSpawn:int;
 var enemiesSpawned:int;
 
 var spawnRate = 1;
 private var nextSpawnTime = 0.0;
 
 function Start () 
 {
     enemiesToSpawn = 3;
 }
 
 function Update () 
 {
     print("next wave time = " + nextWaveTime);
     print("enemies to spawn = " + enemiesToSpawn);
     if(Time.time >= nextWaveTime)
     {
         for(var i:int=enemiesSpawned; i < enemiesToSpawn; i++)
         {
             if(Time.time >= nextSpawnTime)
             {
                 SpawnEnemy();
                 enemiesSpawned++;
                 print("enemies spawned = " + enemiesSpawned); 
                 CheckWave();
             }
         }
     }
     
 }
 
 function CheckWave()
 {
     if(enemiesSpawned >= enemiesToSpawn)
     {
         nextWaveTime = Time.time + waveTime;
         currentWave++;
         enemiesSpawned = 0;
         enemiesToSpawn = 0;
         print("current wave: " + currentWave);
     }
 }
 
 function SpawnEnemy()
 {
     nextSpawnTime = Time.time + spawnRate;
     var newEnemy = Instantiate(enemy, enemySpawn.position, enemySpawn.rotation);
     enemies.Add(newEnemy);
     print("enemies = " + enemies.length);
 }
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

Answer by robertbu · Sep 06, 2014 at 02:42 AM

I think your problem is on line 51:

 enemiesToSpawn = 0;

I do't see any place where you reset it to something other than 0, so in line 30:

 for(var i:int=enemiesSpawned; i < enemiesToSpawn; i++)

enemiesToSpawn will never be greater than 'i' for the second wave.

Comment
Add comment · Show 2 · 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 game0bject · Sep 09, 2014 at 07:59 AM 0
Share

Thanks so much! looking at a problem for such a long period can make you miss the most obvious!

avatar image ThePersister · Sep 09, 2014 at 08:16 AM 0
Share

Yepp, that was it, please accept robertbu's answer to close the question, or if you have any more questions, feel free to ask :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Increase speed over time enemy 1 Answer

How to detect a enemy and change my png according to the enemy position 1 Answer

More Efficient Enemy Spawning Than This? 2 Answers

How to make an enemy attack every few seconds 1 Answer

random enemy spawn from day night cycle 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