Hello dear coders i need your help i made a code whit the help of youtube and i need help the code works perfectly it spaws enemys and waves waits for me to kill all the enemys then starts a new wave but there is one problem whit the code it onky alows me to use one tipe of prefab (enemy) how can i modify the code to spaw multiple tipes of enemys
this is the code
using UnityEngine;
using System.Collections;
public class wawespawner : MonoBehaviour
{
public enum SpawState {Spawning, waiting, counting };
[System.Serializable]
public class Wave
{
public string name;
public Transform enemy;
public int count;
public float rate;
}
public Wave [] Waves;
private int nextWave = 0;
public Transform[] spawnpoints;
public float TimeBetweenWaves = 5f;
private float wawecountdown;
private float Searchcountdown = 1f;
private SpawState state = SpawState.counting;
void Start ()
{
if (spawnpoints.Length == 0)
{
Debug.LogError ("We need SpawPoints");
}
wawecountdown = TimeBetweenWaves;
}
void Update ()
{
if (state == SpawState.waiting)
{
if (!Enemyisalive ())
{
WaveComploted ();
}
else
{
return;
}
}
if (wawecountdown <= 0)
{
if (state != SpawState.Spawning)
{
StartCoroutine (SpawWave (Waves [nextWave]));
}
}
else
{
wawecountdown -= Time.deltaTime;
}
}
void WaveComploted ()
{
Debug.Log ("WaveCompleted");
state = SpawState.counting;
wawecountdown = TimeBetweenWaves;
if (nextWave + 1 > Waves.Length - 1) {
nextWave = 0;
Debug.Log ("We comploted all waves");
}
else
{
nextWave++;
}
}
bool Enemyisalive ()
{
Searchcountdown -= Time.deltaTime;
if ( Searchcountdown <= 0f)
{
Searchcountdown = 1f;
if (GameObject.FindGameObjectWithTag ("Enemy") == null)
{
return false;
}
}
return true;
}
IEnumerator SpawWave (Wave wave)
{
Debug.Log ("SPawming wawe" + wave.name);
state = SpawState.Spawning;
for (int i = 0; i < wave.count; i++)
{
SpawEnemy (wave.enemy);
yield return new WaitForSeconds (1 / wave.rate);
}
state = SpawState.waiting;
yield break;
}
void SpawEnemy (Transform enemy)
{
Debug.Log ("SPawning enemy" + enemy.name);
Transform sp = spawnpoints [Random.Range (0, spawnpoints.Length)];
Instantiate (enemy, sp.position, sp.rotation);
}
}
from a quick glance at the code, it looks like it should be possible. it's dependent on the type of object that you put in the enemy variable in the
– gjfWavesarray. are you populating that in the inspector? you'd have to do something else if you wanted multiple types spawned in a single wave...