• 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 FoxAdventures · Mar 19, 2020 at 04:05 PM · arraywaves

Multiple Objects in Wave Spawner?

Hello everyone, I was doing the Brackey's tutorial on wave spawner. It taught me how to have multiple waves & spawners but the things is, each wave can only spawn ONE type of enemy. I want to have an array of enemies that I can insert into each wave so that each wave can have multiple types of enemies spawning. Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spawnerScript : MonoBehaviour
 {
 
 
     [System.Serializable]
     public class Wave
     {
         public string name;
         public Transform enemy;
         public int count;
         public float rate;
 
     }
 
     public enum SpawnState { SPAWNING, WAITING, COUNTING};
     public SpawnState state = SpawnState.COUNTING;
     public Wave[] waves;
     private int nextWave = 0;
 
     public float timeBetweenWaves = 5f;
     public float waveCountdown;
 
     private float searchCd = 1f;
 
     public Transform[] spawnPoints;
 
     void Start()
     {
         waveCountdown = timeBetweenWaves;
         if (spawnPoints.Length == 0)
             {
                 Debug.LogError("no spawn points");
             }
     }
     void Update()
     {
 
         if (state == SpawnState.WAITING)
         {
             if (!EnemyIsAlive())
             {
                 waveCompleted();
                 return;
             }
             else
             {
                 return;
             }
         }
 
         if (waveCountdown <= 0)
         {
             if(state != SpawnState.SPAWNING)
             {
                 StartCoroutine(SpawnWave(waves[nextWave]));
 
             }
         }
         else
         {
             waveCountdown -= Time.deltaTime;
 
         }
     }
 
 
         void waveCompleted()
         {
             Debug.Log("Wave Completed!!");
             state = SpawnState.COUNTING;
             waveCountdown = timeBetweenWaves;
 
             if (nextWave + 1 > waves.Length - 1)
             {
                 nextWave = 0;
                 Debug.Log("You won. Looping. Good luck.");
             }
             else
             {
             nextWave++;
 
             }
         }
     bool EnemyIsAlive()
     {
         searchCd -= Time.deltaTime;
         if (searchCd <= 0f)
         {
             searchCd = 1f;
 
         
             if (GameObject.FindGameObjectWithTag("Enemy") == null)
             {
             return false;
         
             }
         }
         return true;
     }
 
     IEnumerator SpawnWave(Wave wave)
     {
         Debug.Log("Spawning wave: "+ wave.name);
         state = SpawnState.SPAWNING;
         
 
 
         //SPAWN
 
         for (int i = 0; i <wave.count; i++)
         {
             SpawnEnemy(wave.enemy);
             yield return new WaitForSeconds(1f/wave.rate);
         }
         state = SpawnState.WAITING;
         yield break;
     }
 
     void SpawnEnemy(Transform _enemy)
     {        Debug.Log("Spawning "+ _enemy.name);
             
         Transform spawnP = spawnPoints[Random.Range(0, spawnPoints.Length)];
         Instantiate(_enemy, spawnP.position, Quaternion.identity);
     }
 }


I've never been too great at arrays, and I've been trying to get my head around this for hours. Thanks in advance.

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 Bunny83 · Mar 19, 2020 at 05:32 PM

A long time ago I've written this wave spawner script. Each wave consists of one or multiple "actions". Each action can spawn a variable number of one enemy type. However you can add as many actions to one wave as you want. Just set the delay between those actions to 0 so they essentially happen at the same time.


Note that this is a very simple spawner. You could add another layer of abstraction or use ScriptableObject based actions which you can plug into the spawner. Keep in mind that my spawner does not wait for all the enemies from the current wave to be destroyed. It's purely time based.


Of course if you want to stick to your current script you can simple replace the

 public Transform enemy;

variable by an array like this:

 public Transform[] enemyPrefabs;

Before you call your SpawnEnemy method you have to decide which prefab you want to spawn. Maybe just pick a random prefab from the array or cycle through the array. So either

 Transform enemyPrefab = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];

or just

 Transform enemyPrefab = enemyPrefabs[i % enemyPrefabs.Length];


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 FoxAdventures · Mar 19, 2020 at 09:47 PM 0
Share

Hey, thanks for the reply! When I make "enemy" to an array, and add that bit of code on SpawnEnemy, I'm just kind of lost on what to do next because I get a bunch of errors. This is a real head scratcher for me. In the "SpawnWave" numerator, I need to call SpawnEnemy. What would I be inserting there? I'm REALLY confused. :p

avatar image FoxAdventures · Mar 19, 2020 at 10:19 PM 0
Share

Oh and, your code is actually really cool, but the project I'm working on is actually focused on beating 100 waves, so when they're all intervened I don't think I can separate them properly and show them to the player.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

141 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 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

spawning waves script. 2 Answers

Mouse Position to array 2 Answers

Check if Array.GetValue(i) is null 1 Answer

Changed array type, different functions? 1 Answer

Add Array of Strings to info.plist 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges