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 DYV · Aug 11, 2019 at 02:18 PM · spawnerenemy spawnwaves

wave spawner with different enemies in the waves

There is a lot of wave spawner scripts and tutorials out there but all of them can't spawn few different enemies in one wave. Except this one from Space Shooter tutorial (was just a bit edited by me ). What I want is that I could spawn first wave with some different enemies, but second wave with another different enemies and so on. It could be cool to set for every wave special spawnValues too. My problem is that I have no slightest notion how to make it, my programming skill is just far away from this task. So could someone help me? Please:)

 {
     public GameObject[] hazards;
     public GUI healthBar;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
     public int numberOfWaves;
 
     
     void Start ()
     {
         StartCoroutine (SpawnWaves ());
 
     }
     
     IEnumerator SpawnWaves ()
     {
 
 
             yield return new WaitForSeconds (startWait);
             //while (true)
             while (numberOfWaves > 0) 
         {
                 for (int i = 0; i < hazardCount; i++) {
                     GameObject hazard = hazards [Random.Range (0, hazards.Length)];
                     Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                     Quaternion spawnRotation = Quaternion.identity;
                     Instantiate (hazard, spawnPosition, spawnRotation);
                     yield return new WaitForSeconds (spawnWait);
                 }
 
 
                 yield return new WaitForSeconds (waveWait);
                 numberOfWaves--;
             if (numberOfWaves == 0) 
             {
                 break;
             }
             }
 
     }
 }




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 sacredgeometry · Aug 11, 2019 at 03:57 PM 0
Share

If you are going to change the question after its been answered please notify me so I can update people who have answered so that their answers are still relevant.

If its a completely different question please create a new question because if there are alot of answers it would be really confusing for anyone trying to find similar answers.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sacredgeometry · Aug 11, 2019 at 04:09 PM

Try this

     using System;
     using System.Collections;
     using System.Collections.Generic;
     using System.Linq;
     using UnityEngine;
     using UnityEngine.UI;
     
     [Serializable]
     public class WaveEnemy
     {
         public int Amount;
         public GameObject Type;
     }
     
     [Serializable]
     public class Wave 
     {
         public List<WaveEnemy> Enemy;
     }
     
     public class Spawner : MonoBehaviour
     {    
         public List<Wave> Waves;
         public int CurrentWave = 0;
         
     
         void CreateNextEnemyWave()
         {
             var wave = Waves.ElementAtOrDefault(CurrentWave);
     
             if(wave != null) {
                 CreateEnemiesFor(wave);
                 CurrentWave++; 
             }
         }
     
         void CreateEnemiesFor(Wave wave)
         {
             if(!wave.Enemy.Any()) return;
             
             foreach (var enemy in wave.Enemy)
             {
                  for (int i = 0; i < enemy.Amount; i++)
                 {
                     Instantiate(enemy.Type, Vector3.zero, Quaternion.identity);
                 }   
             }
         }
     
         // To test press spacebar
         void Update()
         {
             if(Input.GetKeyDown(KeyCode.Space)) CreateNextEnemyWave();
         }
     }
 
Comment
Add comment · Show 9 · 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 DYV · Aug 11, 2019 at 04:49 PM 0
Share

thank you for your answers Yes I want each wave to have an exact set of enemies, so it should be 2D array then. Is there a good tutorial to use it?

avatar image sacredgeometry DYV · Aug 11, 2019 at 04:50 PM 0
Share

I can update my code one sec.

avatar image sacredgeometry DYV · Aug 11, 2019 at 05:06 PM 0
Share

@DYV There you go I have updated the answer

avatar image sacredgeometry DYV · Aug 11, 2019 at 05:07 PM 0
Share

I can make a video explaining it if you want.

avatar image DYV sacredgeometry · Aug 11, 2019 at 05:27 PM 0
Share

this script I like more :) Thank you . I have to go sleep at the moment, so I will test the script tomorrow and write you

Show more comments
avatar image
0

Answer by Bunny83 · Aug 11, 2019 at 06:18 PM

There has been countless of such questions already. Here's the one I wrote a long time ago. However it highly depends on your requirements.

Comment
Add comment · Show 1 · 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 DYV · Aug 12, 2019 at 09:52 AM 0
Share

thank you, I saw this but I didn't get that there is ability to spawn different enemies within one wave. Now I see it and going to try to adjust your script to my needs

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

109 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

Related Questions

How do I add multiple different enemies to a wave spawner? 2 Answers

Trouble with Object Pooled Enemy Waves 0 Answers

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

NEED HELP! Wave spawner just wont work!,NEED HELP! Wave spawner not working :( 2 Answers

Enemy Respawn After Death (MMO Style) C# 5 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