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 /
  • Help Room /
avatar image
0
Question by Dilanwizzy · Dec 26, 2016 at 09:40 PM · scripting problemspawningspawning-enemies

TD spawn more than one enemy

Can anyone help me, I am trying tofigure out a way of spawning more than 1 enemy for each waves. Here is my code

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class SpawnWave : MonoBehaviour { public Wave[] waves; public SubWave[] SWave; public EnemyMovent enemy;

 Wave currentWave;
 SubWave SubWa;
 int currentWaveNumber;

 int enemiesRemainingToSpawn;
 int enemiesRemainingAlive;
 float nextSpawnTime;
 int WaitforSpawn = 5;

 void Start()
 {
     Invoke("NextWave", WaitforSpawn);

 }

 void Update()
 {
     Debug.Log(currentWave.enemyCount);
     if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
     {
         enemiesRemainingToSpawn--;
         nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;

         EnemyMovent spawnedEnemy = Instantiate(enemy);
         spawnedEnemy.OnDeath += OnEnemyDeath;
     }
 }

 void OnEnemyDeath()
 {
     enemiesRemainingAlive--;

     if (enemiesRemainingAlive == 0)
     {
         Invoke("NextWave", WaitforSpawn);
     }
 }

 void NextWave()
 {

     currentWaveNumber++;
     print("Wave: " + currentWaveNumber);
     if (currentWaveNumber - 1 < waves.Length)
     {
         currentWave = waves[currentWaveNumber - 1];

         enemiesRemainingToSpawn = currentWave.enemyCount;
         enemiesRemainingAlive = enemiesRemainingToSpawn;
     }
 }

 [System.Serializable]
 public class Wave
 {
     public List<SubWave> subWave = new List<SubWave>();
     public int enemyCount;
     public float timeBetweenSpawns;
 }
 [System.Serializable]
 public class SubWave
 {
     public EnemyMovent Enemy;
     public int count = 1;
 }

}

Spawning 1 enemy is not a problem but I can't figure out how to do more then 1.

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 TBruce · Dec 26, 2016 at 09:41 PM

Here is your modified script to fix your problem (please forgive me for reformatting your code)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SpawnWave : MonoBehaviour
 {
     public List<Wave> waves = new List<Wave>();
     public List<SubWave> SWave = new List<SubWave>();
     public EnemyMovent enemy;
 
     Wave currentWave;
     SubWave SubWa;
 
     // [HideInInspector] // uncomment this to hide in inspector or remove the public
     public int currentWaveNumber = -1;
 
     // [HideInInspector] // uncomment this to hide in inspector or remove the public
     [Range(2, 20)] // change if needeed
     public int enemiesRemainingToSpawn = 5; // change this to desired value - must be greater than 0
 
     // [HideInInspector] // uncomment this to hide in inspector or remove the public
     public int enemiesRemainingAlive = 0; // needs to default to 0
 
     // [HideInInspector] // uncomment this to hide in inspector or remove the public
     public float nextSpawnTime;
 
     // [HideInInspector] // uncomment this to hide in inspector or remove the public
     [Range(0, 10)] // change if needeed
     public int WaitforSpawn = 5;
 
     void Start()
     {
         nextSpawnTime = Time.time;
         nextSpawnTime = Time.time + 3;
         Invoke("NextWave", WaitforSpawn);
     }
 
     void Update()
     {
         if (currentWave != null)
         {
             if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
             {
                 enemiesRemainingToSpawn--;
                 nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
 
                 EnemyMovent spawnedEnemy = Instantiate(enemy);
                 enemiesRemainingAlive++;
                 
                 // you may want to randomly place the spawned object somewhere on the screen like so
                 // Vector3 position = new Vector3(Random.Range(-9f, 9f), Random.Range(-5f, 5f), 0);
                 // spawnedEnemy.gameObject.transform.position = position;
                 spawnedEnemy.OnDeath += OnEnemyDeath;
             }
         }
     }
 
     void OnEnemyDeath()
     {
         enemiesRemainingAlive--;
         enemiesRemainingToSpawn++;
 
         if (enemiesRemainingAlive == 0)
         {
             Invoke("NextWave", WaitforSpawn);
         }
     }
 
     void NextWave()
     {
         if (currentWaveNumber < waves.Count)
         {
             waves.Add(new Wave());
             currentWaveNumber = waves.Count - 1;
             currentWave = waves[currentWaveNumber];
             enemiesRemainingToSpawn = currentWave.enemyCount;
         }
         currentWaveNumber++;
         print("Wave: " + currentWaveNumber);
     }
 
     [System.Serializable]
     public class Wave
     {
         public List<SubWave> subWave = new List<SubWave>();
         public int enemyCount = 5;          // you need a value here
         public float timeBetweenSpawns = 3; // you need a value here
     }
 
     [System.Serializable]
     public class SubWave
     {
         public EnemyMovent Enemy;
         public int count = 1;
     }
 }

Since I did not have the EnemyMovent class I have to create a dummy one with a delagate for testing purposes.

Comment
Add comment · Show 3 · 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 TBruce · Dec 27, 2016 at 07:40 PM 0
Share

Hi @Dilanwizzy, Did this solve your problem?

avatar image Dilanwizzy · Dec 29, 2016 at 11:06 PM 0
Share

Hi, not really. In the code I can have multiple waves of enemies but I want to be able to spawn different enemies in a single wave. The code only spawns one type of enemy.

I figured out a way to do it but it is very inefficient and it might not be good for performance. I am just using it until I figure out a better way.

avatar image TBruce Dilanwizzy · Dec 31, 2016 at 11:33 PM 0
Share

Sorry to hear that. I$$anonymous$$HO, I would not have gone the route that your code is written but I did not want to deviate from what you were currently using to make it incompatible with your current system nor is it evident from your original question or script (your script only has one enemy prefab) that you wanted the ability to spawn more that one type of enemies or the method (is each enemy spawned selected from a random prefab).

Also since this is only a portion of your project there is still a lot that I do not understand (e.g. the extra classes that have no real functionality, you do not have a method for destroying enemies, I do not know what your OnDeath listener looks like or how it functions - to name a few).

Happy New Year!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make the enemies continue spawning? 1 Answer

I need help with my spawning system(SOLVED) 1 Answer

my destroy wall code keeps destroying entire object for good 2 Answers

Buncha questions about scripts. 0 Answers

problem when i spawn to many Enemy the game be slower 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