Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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 /
avatar image
0
Question by Vannerss · Apr 01 at 02:38 AM · instantiateloopingenemy spawnspawning-enemies

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

I'm currently making a small co-op round base zombie shooter for educational purposes and I'm trying to spawn a total amount of Zombies per round but limit the amount of zombies that can be alive at the same time so that when you get to later round the game doesn't crash for having too many zombies.

i.e at round 20 the total amount of zombies spawning is 100 but only 25 zombies can be alive at the same time. So i would like that when i reach the cap of zombies, the rest of the 75 zombies don't spawn until the next one dies.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyManager : MonoBehaviour
 {
     //-------------singleton-----------------
     public static EnemyManager instance;
 
     private GameManager gameManager;
 
     public GameObject enemyPrefab;
     public Transform[] spawnPoints;
 
     public delegate void AllZombiesKilled();
     public static event AllZombiesKilled onAllZombiesKilled;
     
     const int maxZombieSpawn = 25;
     const float difficultyMultiplier = 0.15f;
 
     int amountToSpawn;
     int maxAmountofZ;
 
     private void OnEnable()
     {
         GameManager.onRoundStarted += SpawnZombies;
     }
 
     private void OnDisable()
     {
         GameManager.onRoundStarted -= SpawnZombies;
     }
 
     private void Awake()
     {
         if (instance == null)
             instance = this;
         else if (instance != this)
             Destroy(gameObject);
     }
 
     void Start()
     {
         gameManager = GameManager.instance;
     }
 
     void SpawnNewEnemy(int amount)
     {
         for(int i = amount; i > 0; i--)
         {
             int holder = i;
             if (GameObject.FindGameObjectsWithTag("Enemy").Length < maxZombieSpawn)
             {
                 Instantiate(enemyPrefab, spawnPoints[0].transform.position, Quaternion.identity);
             }
             else
             {
                 i = holder + 1;
             }
         }
     }
 
     void SpawnZombies()
     {
         switch (gameManager.currentRound)
         {
             case 1:
                 Debug.Log("case 1");
                 amountToSpawn = Mathf.RoundToInt(maxAmountofZ * 0.2f);
                 SpawnNewEnemy(amountToSpawn);
                 break;
             case 2:
                 amountToSpawn = Mathf.RoundToInt(maxAmountofZ * 0.4f);
                 SpawnNewEnemy(amountToSpawn);
                 break;
             case 3:
                 amountToSpawn = Mathf.RoundToInt(maxAmountofZ * 0.6f);
                 SpawnNewEnemy(amountToSpawn);
                 break;
             case 4:
                 amountToSpawn = Mathf.RoundToInt(maxAmountofZ * 0.8f);
                 SpawnNewEnemy(amountToSpawn);
                 break;
             case int n when (n >= 5 && n <= 9):
                 amountToSpawn = maxAmountofZ;
                 SpawnNewEnemy(amountToSpawn);
                 break;
             case int n when (n >= 10):
                 amountToSpawn = Mathf.RoundToInt((gameManager.gameRound * difficultyMultiplier) * maxAmountofZ);
                 SpawnNewEnemy(amountToSpawn);
                 break;
             default:
                 break;
         }
     }
 }

my solution was to ad the if-statement inside the for-loop but when the I try spawning a number of enemies higher than the maxAmount my whole unity freezes without even saying it crashed. A complete stop.

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

0 Replies

· Add your reply
  • Sort: 

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

157 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 avatar image avatar image avatar image avatar image 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 can I make sure my GameObjects don't spawn on top of each other, and that only a certain number remain in the scene at one time? 1 Answer

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

Having trouble with an enemy spawn script 1 Answer

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

Instantiating at different rates 1 Answer


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