• 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 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
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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 all humans at once and activate then with time? 1 Answer

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

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges