Enemy Waves

Updated on February 2, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on February 2, 2016

Hi everyone,

I have 4 spawns in my scene, all with the same “IASpawn” script attached. The idea is to spawn 4-5 enemies from each spawn when there are zero enemies on the battlefield. Therefore, when all enemies are killed, the spawn script will instantiate another set of enemies. I’m using the following script:

 using UnityEngine;
 using System.Collections;
 public class IASpawn : MonoBehaviour {
 public Rigidbody2D Bot;
 public float spawnDivision = 130f;
 private float counter = 1f;
 public float maxCounterValue = 390;
 public int i = 0;
 // void Start () {
 // Rigidbody2D IA;
 // IA = Instantiate (Bot, transform.position, transform.rotation) as Rigidbody2D;
 // }
 void Update(){
 if (GameObject.FindWithTag ("Enemies") == null) {
 while (i < 5) {
 Invoke("Spawn", 0f);
 i += 1;
 }
 }
 }
 void Spawn(){
 if (counter%spawnDivision == 0){
 if (counter <= maxCounterValue) {
 Rigidbody2D IA;
 IA = Instantiate (Bot, transform.position, transform.rotation) as Rigidbody2D;
 counter += 1;
 }
 else {
 counter = 1;
 }
 }
 else{
 counter += 1;
 }
 }
 }
 

“Bot” is a prefab of the enemy and it has tag “Enemies”. I know that the “Spawn” method is working because I tested it, but unfortunately nothing happens when the game starts, so it’s the “Update” method which has some error. Any help please? Thx in advance

  • Liked by
Reply
1 on February 2, 2016

Try using GameObject instead of Rigidbody in line 4 ,and try & also try using a Gamemanager were it will keep information of bots, since using .FindwithTag will increase the memory consumption .

Wise
on February 2, 2016
  • Also on Line 24 and Line 25 change it to GameObject.
  • If the enemies are of same type, try to implement object pooling
Show more replies
  • Liked by
Reply
Cancel
0 on February 2, 2016

Okay, thanks to both!

  • Liked by
Reply
Cancel