Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 kubaignatowicz1 · Jan 04 at 10:07 AM · waitforsecondsnoob

How to make waves of enemys spawn after few seconds.

Hi, I picked up unity less than week ago so I don't realy know what's wrong with this code. I've read that you can't use WaitForSeconds();in Update but I don't know why and I don't know how to do it otherwise. Can somebody help me how should I do it?

 public class Spawning : MonoBehaviour
 {
     public GameObject zombiePrefab;
     int randomSpawnZone;
     float randomPositionX, randomPositionY;
     Vector3 spawnPosition;
     public float enemySpawned = 5f;
     public float enemysToKill = 5;
     public float enemyAddedEveryRound = 1;
     
     void Start ()
     {
         for (int i = 0; i < enemySpawned; i++)
         {
             SpawnNewEnemy();
         }
     }
     
     void Update ()
     {
         EnemyWaveSpawner();
     }
     
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(3);
     }
     
     void SpawnNewEnemy()
     {
         randomSpawnZone = Random.Range(0, 4);
 
         switch (randomSpawnZone)
         {
             case 0:
                 randomPositionX = Random.Range(-10f, 10f);
                 randomPositionY = Random.Range(-6f, -5f);
                 break;
             case 1:
                 randomPositionX = Random.Range(-10f, 10f);
                 randomPositionY = Random.Range(5f, 6f);
                 break;
             case 2:
                 randomPositionX = Random.Range(10f, 11f);
                 randomPositionY = Random.Range(-5f, 5f);
                 break;
             default:
                 randomPositionX = Random.Range(-11f, -10f);
                 randomPositionY = Random.Range(-5f, 5f);
                 break;
         }
         spawnPosition = new Vector3(randomPositionX, randomPositionY, 0f);
         Instantiate(zombiePrefab, spawnPosition, Quaternion.identity);
     }
     
     void EnemyWaveSpawner()
     {
         if (enemysToKill == 0)
         {
             Wait();
             enemySpawned += enemyAddedEveryRound;
             enemysToKill = enemySpawned;
             for (int i = 0; i < enemySpawned; i++)
             {
                 SpawnNewEnemy();
             }
         }
     }
     
 }
Comment
Add comment · Show 2
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 andrew-lukasik · Jan 04 at 08:26 AM 0
Share

If you want to execute a code-block over time like this, the thing you need is called Coroutine. You were partially on a right track there but the code didn't make much sense. Paczaj na to instead:

 public class Spawning : MonoBehaviour
 {
     [SerializeField] GameObject zombiePrefab;
     public int enemyAddedEveryRound = 5;
     public int enemiesSpawned = 0;
     
     void Start ()
     {
         StartCoroutine( EnemyWaveSpawnerRoutine() );
     }
     
     IEnumerator EnemyWaveSpawnerRoutine ()
     {
         SpawnWave();
 
         while( enemiesSpawned>0 )
         {
             SpawnWave();
             yield return new WaitForSeconds(3);
         }
     }
 
     void SpawnWave ()
     {
         for( int i=0 ; i<enemyAddedEveryRound ; i++ )
         {
             SpawnEnemy();
         }
         enemiesSpawned += enemyAddedEveryRound;
     }
     
     void SpawnEnemy ()
     {
         int randomSpawnZone = Random.Range(0, 4);
         float randomPosX;
         float randomPosY;
         switch( randomSpawnZone )
         {
             case 0:
                 randomPosX = Random.Range(-10f, 10f);
                 randomPosY = Random.Range(-6f, -5f);
                 break;
             case 1:
                 randomPosX = Random.Range(-10f, 10f);
                 randomPosY = Random.Range(5f, 6f);
                 break;
             case 2:
                 randomPosX = Random.Range(10f, 11f);
                 randomPosY = Random.Range(-5f, 5f);
                 break;
             default:
                 randomPosX = Random.Range(-11f, -10f);
                 randomPosY = Random.Range(-5f, 5f);
                 break;
         }
         Vector3 spawnPosition = new Vector3( randomPosX , randomPosY , 0f );
         Instantiate( zombiePrefab , spawnPosition , Quaternion.identity );
     }
 
 }
avatar image kubaignatowicz1 andrew-lukasik · Jan 04 at 03:33 PM 1
Share

Dziękuje bardzo :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by henkehedstrom · Jan 07 at 03:38 PM

You need to write StartCoroutine(COROUTINE()); So instead of calling the Wait function like Wait(); you have to write StartCoroutine(Wait());


You cannot write yield return new WaitForSeconds() in the update function becuse it returns void compared to your Wait function that returns an IEnumerator. I am not that knowledgeable about how coroutines actually work but here are some links if you want to learn more. https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html https://docs.unity3d.com/ScriptReference/Coroutine.html

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

139 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

Related Questions

Trying to delay code, nothing works. 2 Answers

IENumerator does not work 0 Answers

Problem with coroutine / create a gap of 5 seconds [C#] 1 Answer

How to implement pausing of WaitForSeconds based coroutine? 1 Answer

Call Coroutine step after step in foreach loop 0 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