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 Brentonk · Mar 16, 2017 at 08:37 PM · spawnspawning-enemies

Multiple Enemy Wave Spawners?

I have a working script where at the start my set number of max enemies will Spawn, and upon killing them the next wave starts and spawns the same number of enemies. However when I put in multiple spawners with this script, the enemies only spawn out of one spawner. How can I use this wave spawning system across multiple spawners?

This is my current Wave Spawning Script:

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

public class EnemySpawnWaveBased : MonoBehaviour {

 public int totalWaves = 5;
 private int numWaves = 0;
 private bool waveSpawn;
 public bool Spawn = true;
 public SpawnTypes spawnType = SpawnTypes.Wave;

 public int totalEnemy = 10;
 public static int numEnemy;

 public GameObject Enemy;

 void Start () {
 }

 void Update () {
     
     Debug.Log(waveSpawn);

     GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
     numEnemy = enemies.Length;

     if(Spawn)
     {
         if (spawnType == SpawnTypes.Wave)
         {
             if(numWaves < totalWaves + 1)
             {
                 if (numEnemy == 0)
                 {
                     // enables the wave spawner
                     waveSpawn = true;
                     //increase the number of waves
                     numWaves++;
                 }
                 if (waveSpawn)
                 {
                     //spawns an enemy
                     spawnEnemy();
                 }
                 if(numEnemy >= totalEnemy)
                 {
                     // disables the wave spawner
                     waveSpawn = false;
                 }
             }
         }
     }
 }

 private void spawnEnemy()
 {
         if (waveSpawn == true) {
             Instantiate (Enemy, gameObject.transform.position, Quaternion.identity);
         }
 }

 public enum SpawnTypes
 {
     Wave
 }

}

Comment
Add comment · Show 1
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 rhylvin2016 · Mar 17, 2017 at 01:12 AM 0
Share

are you sure you're putting this script inside that new spawner?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kuivalappa · Mar 17, 2017 at 02:24 PM

try this spawner

public class Spawner1 : MonoBehaviour {

 [System.Serializable]
 public class Wave
 {

     public GameObject enemy;
     public int count;
     public float rate;
     public Transform spawnPoint;
    

 }

 public static int EnemiesAlive;

 public Wave[] waves;

 public static Transform spawnPoint;

 public float timeBetweenWaves = 5f;
 private  float countdown = 2f;

 public Text waveCountdownText;


 private int waveIndex = 0;


 void Update()
 {

        if (EnemiesAlive > 0)
     {
         
     }

     if (waveIndex == waves.Length)
     {
      
         this.enabled = false;
     }

   

     if (countdown <= 0f)
     {
         StartCoroutine(SpawnWave());
         countdown = timeBetweenWaves;
         return;
     }

     countdown -= Time.deltaTime;

     countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);

     waveCountdownText.text = string.Format("{0:00.00}", countdown);
 }

 IEnumerator SpawnWave()
 {
   

     Wave wave = waves[waveIndex];

     EnemiesAlive = wave.count;


     for (int i = 0; i < wave.count; i++)
     {
         SpawnEnemy(wave.enemy);
         yield return new WaitForSeconds(1f / wave.rate);
     }

     waveIndex++;

 }

 void SpawnEnemy(GameObject enemy)
 {
      Wave wave = waves[waveIndex];

      spawnPoint = wave.spawnPoint;

     Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
 }

}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn Random Enemies 2D 1 Answer

Spawn Point 2 Answers

Spawn waves & wait till it's killed, spawn again. 1 Answer

Enemy Spawn System with time and limit enemy in game 2 Answers

How to spawn enemies at different locations and avoid overlapping each other 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