• 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 fintan22 · Jan 27, 2020 at 11:39 PM · instantiatespawningmaximum

max amount of enemy present?

in t$$anonymous$$s code i spawn from an array of enemy into my game. can someone help me so that i can spawn a max amount of enemy for each type of enemy. and that is spawns them instantly. and if one is destroyed another spawns in

public class enemyspawn : MonoBehaviour { //Array of enemies to spawn public GameObject[] enemy;

 //Enemies will be spawned in random positions inside these boundaries
 //The minimum x and z position to spawn the enemies 
 public Vector2 minSpawnPos;
 //The maximum x and z position to spawn the enemies 
 public Vector2 maxSpawnPos;

 //The time between enemy spawns
 public float spawnRate = 3f;
 //The minimum distance to spawn the last spawned enemy
 public float minSpawnDistance = 2f;

 Vector3 lastSpawnPosition;
 float lastSpawn;

 private void Update()
 {
     //Time check to see if it's time to spawn a new enemy
     if (lastSpawn + spawnRate < Time.time)
     {
         SpawnEnemy();
     }
 }
 private void SpawnEnemy()
 {
     Vector3 spawnPosition;
     //Selects a random enemy to spawn from the array of enemies
     int enemySpawnIndex = Random.Range(0, enemy.Length);

     //Will keep on generating a new spawn position until it's far enough away from the last one
     do
     {
         spawnPosition = new Vector3(Random.Range(minSpawnPos.x, maxSpawnPos.x), Random.Range(minSpawnPos.y, maxSpawnPos.y));
     } w$$anonymous$$le (Vector3.Distance(spawnPosition, lastSpawnPosition) < minSpawnDistance);

     //Spawns a new instance of an enemy
     GameObject instance = Instantiate(enemy[enemySpawnIndex], spawnPosition, Quaternion.identity);

     //"Resets" the spawn timer
     lastSpawn = Time.time;
     //Sets t$$anonymous$$s enemies position as the last spawned enemy
     lastSpawnPosition = spawnPosition;
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Magso · Jan 28, 2020 at 01:08 AM

T$$anonymous$$s can be done with a for loop by setting a variable to how many enemies there should be and checking if FindGameObjectsWithTag length exceeds t$$anonymous$$s value.

 var enemies = GameObject.FindGameObjectsWithTag("Enemy");
 if(enemies.Length < maximumEnemies)
     {
     var enemiesNeeded = maximumEnemies - enemies.Length;
     for(int i = 0; i < enemiesNeeded; i++)
         {
         //SpawnEnemy
         }
     }

Comment
Add comment · Show 4 · 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 fintan22 · Jan 28, 2020 at 07:11 AM 0
Share

there are red lines all over this in my code

avatar image Magso fintan22 · Jan 28, 2020 at 10:28 AM 0
Share

Red lines? Compiler error or (I suspect) visual studio red lining a unity specific method like FindGameObjectsWithTag and variable names?

avatar image fintan22 · Jan 28, 2020 at 07:12 AM 0
Share

there are red lines all over this in my code

avatar image logicandchaos · Jan 28, 2020 at 02:11 PM 0
Share

maybe try hard typing instead of using so many vars

avatar image
0

Answer by logicandchaos · Jan 28, 2020 at 02:15 PM

     int maxEnemies=100;
     Start(){
     for(int i=0;i<maxEnemies;i++)
     Instantiate();
 }
 

Then when an enemy dies instead of destroying it just move it somewhere else, to a spawn location.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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

Related Questions

How to Make an Expanding Spawn Area for Prefabs? 2 Answers

Add an offset distance between 2 spawned objects 1 Answer

How to Spawn after checking if the clones are destroyed. 1 Answer

Spawner won't work if I click too fast 0 Answers

Players Not Showing 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