• 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
Question by danteandwes · Nov 06, 2020 at 10:50 AM · spawnspawning problemsspawnerspawning-enemies

SPAWNING ENEMIES and controlling the amount/interval

I am trying to spawn enemies from the top of the screen and fall down where they are destroyed at the bottom. How can I change my scipt to control the amount of objects that fall and how often they fall? currently I have methods in place but they dont really seem funtional. See my code below.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EnemySpawner : MonoBehaviour { [SerializeField] private float xLimit = 0; //spawns single enemy [SerializeField] private float [] xPositions = null; [SerializeField] private GameObject[] enemyPrefabs = null; [SerializeField] private Wave[] wave = null;

 private float currentTime;

 List<float> remainingPositions = new List<float>();
 private int waveIndex;
 float xPos = 0;
 int rand;
 // Start is called before the first frame update
 void Start()
 {
    
     currentTime = 0;
     remainingPositions.AddRange(xPositions);
 }

 // Update is called once per frame
 void Update()
 {
     
     currentTime -=  Time.deltaTime;
     if(currentTime <= 0)
     {
         SelectWave();
     }
 }
 void SpawnEnemy(float xPos)
 {
     //gameObject.transform.GetComponent<Rigidbody2D>().AddForce(Vector2.down*30*Time.deltaTime,ForceMode2D.Force); 
     int r = Random.Range(0,8); //3 types of enemies so increase if needed
     GameObject enemyObj = Instantiate(enemyPrefabs[r], new Vector3(xPos,transform.position.y,0),Quaternion.identity);//can add rotation here
     /*string enemyName = "";
     if (r == 0) enemyName = "realVirus-removebg-preview";
     else if (r == 1) enemyName = "realVirus-removebg-preview (2)";
     else if (r == 2) enemyName = "NormalBlue";
     else if (r == 3) enemyName = "Bomb";
     else if (r == 4) enemyName = "coins";
     else if (r == 5) enemyName = "SlowTime";
     else if (r == 6) enemyName = "Doubler";
     else if (r == 7) enemyName = "Fivex";

     GameObject enemy = ObjectPooling.GetPooledObject(enemyName);
     enemy.transform.position = new Vector3(xPos, transform.position.y,0);
     enemy.SetActive(true);*/
     
 }
 private void SelectWave()
 {
     remainingPositions = new List<float>();
     remainingPositions.AddRange(xPositions);

     waveIndex = Random.Range(0,wave.Length);

     currentTime = wave[waveIndex].delayTime;

     if(wave[waveIndex].spawnAmount == 1)
     {
         xPos = Random.Range(-xLimit,xLimit);
     }
     else if (wave[waveIndex].spawnAmount > 1)
     {
         rand = Random.Range(0, remainingPositions.Count);
         xPos = remainingPositions[rand];
         remainingPositions.RemoveAt(rand); //no overlapping
     }

     for(int i = 0; i < wave[waveIndex].spawnAmount; i++)
     {
         SpawnEnemy(xPos);
         rand = Random.Range(0, remainingPositions.Count);
         xPos = remainingPositions[rand];
         remainingPositions.RemoveAt(rand);
     }
 }

}

[System.Serializable] public class Wave { public float delayTime; public float spawnAmount; },

Comment

People who like this

0 Show 0
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

Answer by Revolution_Game · Nov 20, 2020 at 10:35 AM

First Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemySpawner : MonoBehaviour
 {
     public static int enemyNum = 0;
     float spawnTime = 1.0f;
     bool spawn = true;
     public GameObject blue;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if(enemyNum < 5 && spawn)
         {
             StartCoroutine(spawnEnemy());
         }
     }
     IEnumerator spawnEnemy()
     {
         //spawn your enemy here
         enemyNum += 1;
         spawn = false;
         yield return new WaitForSeconds(spawnTime);
         spawn = true;
     }
 }

Destroy Enemy Script:

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;

  public class DestroyEnemy : MonoBehaviour
 {
 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     //your destroy enemy script here
     EnemySpawner.enemyNum -= 1;
 }
 }
Comment

People who like this

0 Show 0 · 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

141 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

Related Questions

How to spawn enemies at different locations and avoid overlapping each other 2 Answers

How to spawn all humans at once and activate then with time? 1 Answer

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

How to increase the spawn rate of an object over time? 2 Answers

Spawning help 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