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 MathewJProductions · Feb 15, 2019 at 10:10 PM · instantiateenemy spawnlimitations

Putting a limit on the amount of times you can instantiate an Enemy

Hey there,I'm relatively new to unity and I've been trying to create an Enemy Spawner which will spawn enemies over time. I'm trying to create a limit to the number of enemies that can be alive at once but i cant seem to get it working. If anyone can help me that would be awesome.

The code for the Enemy Spawner:

     [Header ("Floats")]
     public float delay;
     public float fastestDelay;
     public float mediumDelay;
     
     public static int amountSpawned = 0;
 
 
     [Header ("Spawning Stuff")]
     public int maxAmountToSpawn = 20;
 
     [Space]
     public Transform[] spawnPoints;
     [Space]
     public GameObject[] enemies;
 
     int randomSpawnPoint, randomEnemy;
     public bool spawnAllowed;
 
     void Start()
     {
         amountSpawned = 0;
         StartCoroutine(SpawnAnEnemy());
         spawnAllowed = true;
     }
 
     private void Update()
     {
         if (amountSpawned <= 19)
         {
             spawnAllowed = true;
         }
     }
 
     IEnumerator SpawnAnEnemy()
     {
         while (true)
         {
            
             if (amountSpawned == maxAmountToSpawn)
             {
                 Debug.Log("Max amount has been reached");
                 spawnAllowed = false;
             }
 
             if (Enemy.EnemyDied == true)
             {
                 if(amountSpawned == 0)
                 {
                     amountSpawned = 0;
                 }
 
                 amountSpawned--;
             }
 
             
 
             randomSpawnPoint = Random.Range(0, spawnPoints.Length);
             randomEnemy = Random.Range(0, enemies.Length);
             Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
 
             amountSpawned++;
             Debug.Log(amountSpawned);
 
             yield return new WaitForSeconds(delay);
 
             delay -= 0.1f;//0.5 for example be carefull not make it less than 0
 
             if (delay <= mediumDelay)
             {
                 PlayerController.amountOfPointsToCount = PlayerController.morePointsToCount;
                 delay -= 0.05f;
             }
 
             if (delay <= fastestDelay) delay = fastestDelay;
 
             while (!spawnAllowed)
                 yield return null;
         }
     }
 
 }

With what I've made it is working to an extent. As I kill an enemy it takes one off the amount spawned which works great until I get to 20. Once I get to 20 and I kill an enemy it doesn't spawn anymore and I can't work out what's happened. In the Enemy script, I have a public static bool which is triggered when the health is set to 0 and the enemy dies. I thought I could use this to know when an enemy has died to then subtract one from the amount spawned.

What I need to do is make it so it starts spawning again once the amount spawned goes back down from 20. If you could help me out id really appreciate it.

Thank you for your time.

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
Best Answer

Answer by xxmariofer · Feb 15, 2019 at 10:16 PM

hello, your problem is not understabding how statics fields work, each enemy doesnt have one var called EnemyDied but the class has one, so you are changing all the time 1 var, so probably thats one of the issues, also i dont understand what you are trying to do here

             if(amountSpawned == 0)
              {
                  amountSpawned = 0;
              }

you are setting amountSpawned to only if its already 0?

also you are using this while as an if

  while (!spawnAllowed)
              yield return null;

and you are never exiting the loop so is being excuted even if you cant spawn

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 MathewJProductions · Feb 15, 2019 at 10:26 PM 0
Share

Sorry, How would you set it so after instantiating 20 enemies you cant instantiate anymore until an enemy is killed. Without the mess I had made here's the script ( when it works properly ):

     [Header ("Floats")]
     public float delay;
     public float fastestDelay;
     public float mediumDelay;
 
     [Header("SpawnPoints")]
     public Transform[] spawnPoints;
     public GameObject[] enemies;
 
     int randomSpawnPoint, randomEnemy;
     public bool spawnAllowed;
 
     void Start()
     {
         StartCoroutine(SpawnAnEnemy());
         spawnAllowed = true;
     }
 
 
     IEnumerator SpawnAnEnemy()
     {
         while (true)
         {
             while (!spawnAllowed)
                 yield return null;
 
             randomSpawnPoint = Random.Range(0, spawnPoints.Length);
             randomEnemy = Random.Range(0, enemies.Length);
             Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
 
             yield return new WaitForSeconds(delay);
 
             delay -= 0.1f; //0.5 for example be carefull not make it less than 0
 
             if (delay <= mediumDelay)
             {
                 // Player Points Stuff Here
                 delay -= 0.05f;
             }
 
             if (delay <= fastestDelay) delay = fastestDelay;         
         }
     }
 }

Does that make more sense?

avatar image xxmariofer MathewJProductions · Feb 15, 2019 at 10:52 PM 0
Share

hello, that should be just as easy as a countdown create a var called something like int numberOfEnemies; in the start set it to 0 and behind the instantiate just add add one enemy nomberOfEnemies++; the second while just change it to an if, that is more standart and that should be it, test it and tell me if its giving any errors.

avatar image MathewJProductions xxmariofer · Feb 15, 2019 at 11:05 PM 0
Share

Yup it all works with no errors, I've been trying to figure out a way to do it for a while now and I guess I overcomplicated it. Thank you.

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

122 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

Related Questions

Respawning Single Enemy at a Random Range 1 Answer

How to spawn objects but set a cap of how many can be alive at the same time? 0 Answers

Instantiating at different rates 1 Answer

Having trouble with an enemy spawn script 1 Answer

Invoke Ball Obstacle based on Score 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