Spawning Waves

Hi, i am trying to make Spawning waves and i get alot of troubles!
This is my script
Right now the script summon a wave every 20 seconds.
i want to summon the second wave only after the monsters are killed or i call it.

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



public class SummonScript : MonoBehaviour {

    [SerializeField]
    public GameObject Target, Drops;
    public CameraController Cam;
    public GameObject[] Enemy = new GameObject[1];
    public Transform Position;
    public Text WaveText, MonstersText,TimeText;
    public NewPlayer Player;
   

    public int[] Monsters = new int[3]; // How many monsters in a wave.
    public int WaveNumber,NumberOfWaves;
  
    public bool Waving;
    private float CounterBetweenWaves, CounterBetweenSpawn;
 
    // Use this for initialization

    void Start () {

        CounterBetweenWaves = 100;
        WaveNumber = 0;
       }
   

    void Update()
    {
        UpdateUI();


        if (CounterBetweenWaves == 0)
        {
            CounterBetweenWaves = 1400;

            if (Monsters.Length-1 > WaveNumber) { WaveNumber++; }
            Waving = true;
        }

        if (Waving)
        {
            if (CounterBetweenSpawn <= 0)
            {
                if (Monsters[WaveNumber] > 0)
                {
                    Summon(0);
                    Monsters[WaveNumber]--;
                    CounterBetweenSpawn = 100;
                }
                else { Waving = false; }               // CounterBetweenWaves = TimeBetweenWaves; }
            }
            else CounterBetweenSpawn--;
        }
        if (!Waving)
        {
            CounterBetweenWaves--;
        }
      
    }
    public void Summon(int ID)
    {
       
        GameObject tmp = (GameObject)Instantiate(Enemy[ID], Position.transform.position , Quaternion.identity) as GameObject;
        tmp.GetComponent<NewEnemy>().Target = Target;
        tmp.GetComponent<NewEnemy>().Cam = Cam;
        tmp.GetComponent<NewEnemy>().Drops = Drops;
        tmp.GetComponent<NewEnemy>().HeroID = ID;
       
    }
    public void UpdateUI()
    {
        WaveText.text ="Wave: " + WaveNumber.ToString() + "/" + (Monsters.Length-1).ToString();
        MonstersText.text ="Monsters to Spawn: " + Monsters[WaveNumber].ToString();
      
        TimeText.text = "Next Wave in: " + (CounterBetweenWaves/60).ToString("0") ;

    }


}

You need to track your spawns. When you instantiate an enemy, add 1 to a variable. Then if an enemy dies, subtract one. Then before you start subtracting from CounterBetweenWaves, check that variable to see if it’s <= 0.

Yea i actually solved it right now.
I tryed it before but it didnt work, probably too tierd lol.

Glad you got it working!
Yeah, programming while being tired is not a good thing. :slight_smile:

But my solution was not enough
I used this function to check how many monsters i have every wave;

    public void CheckIf()
    {
        for(int i = 0;i < Monsters.Length -1 ; i++)
        {
            MonstersSaveValue[i] = Monsters[i];
        }
    }

then i asked if the ammount of death equal to ammount of monsters

if (MonstersSaveValue[WaveNumber] - MonsterKilled == 0)
        {
            StartCount = true;
        }

It works good but at the 3rd wave im having an issue, it tells me im out of range.

    public int[] Monsters = new int[3]; // How many monsters in a wave.
    public int[] MonstersSaveValue = new int[3]; // How many monsters in a wave.

Solved for sure now XD
I always post my problem and 2 seconds after it i find the answer.
I should have changed the inspector’s array size.

You should use time to keep track of spawning waves rather than counting frames. If one computer runs at 30 FPS and the other at 60 FPS, the time between spawning waves is going to be radically different.

Ugh this. So many times the next day I ask, “Why the hell did I do that?”

Hmm, im developing for android if it matters.
And i have pase controller.
How can i use time instead of counters?

He’s suggesting that you use a float to store either the next time for a wave, or the time between waves (I imagine either would work).
Then, you can adjust this time as need : set to a new value, subtract deltaTime from it, etc…
Check if the next wave time has either expired or is <= Time.time (depending if you use a counter counting down or a time to indicate the next wave*).

Basically, translate what ‘100’ or ‘1400’ counters would have meant to you into time (seconds or a fraction thereof)…:slight_smile:

I know this is late, but here is another solution.

Create your monsters with a callback routine like so:

	// on your mob's health
	public int health = 100;
	public delegate void MobDied();

	public void Damage(int amount){
		health -= amount;
		health = Mathf.Max (health, 0);
		if (health == 0) {
			MobDied ();
		}
	}


	// on your spawn controller
	private int mobsSpawned;

	public void CreateMonster(Monster monster, Vector3 position, Quaternion rotation){
		var mob = (GameObject)GameObject.Instantiate (monster, position, rotation);
		var mobHealth = mob.GetComponent<Health> ();
		mobHealth.MobDied = new MobDied(OnDeath);
		mobsSpawned++;
	}

	public void SpawnMobs(int n, Monster monster){
		n = Math.Max(n, 1);
		for(var i=0; i<n; i++){
			CreateMonster (monster, Vector3.zero, Quaternion.identity);
		}
	}

	public void OnDeath(){
		mobsSpawned--;

		if (mobsSpawned == 0) {
			SpawnMobs(10);
		}
	}

basically, you use a callback to tell the spawner when all your monsters are dead, and when they are, just spawn more. The player wont mind at all. :wink: