Hi! I created a basic enemy spawner using this cool tutorial. It essentially spawns waves of the same enemies that have certain properties adjusted in the inspector.
However it does not explain how to add more than one type of enemy in a single wave.
I have tried creating a list and to use instead of using a single enemy but i get the error message: “Argument 1: cannot convert from ‘UnityEngine.GameObject’ to ‘UnityEngine.Transform’”.
Can I make it work by using a list or and if not how why I make multi type enemy waves?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyspawner : MonoBehaviour {
public enum spawnstate { SPAWNING,WAITING,COUTING};
[System.Serializable]
public class Wave
{
public string name;
public GameObject[] enemy;
//public Transform enemy;
public int count;
public float delay = 0.5f;
}
public Wave[] waves;
private int nextwave = 0;
public float timebetweenwaves = 5;
public float wavecountdown;
private float searchCountdown = 1f;
private spawnstate state = spawnstate.COUTING;
public Transform[] Spawnpoints;
// Use this for initialization
void Start () {
wavecountdown = timebetweenwaves;
}
// Update is called once per frame
void Update () {
if (state == spawnstate.WAITING)
{
if (!EnemyisAlive())
{
BeginNewRound();
return;
}
else
{
return;
}
}
if (wavecountdown <= 0)
{
if (state != spawnstate.SPAWNING)
{
StartCoroutine(spawnwave(waves[nextwave]));
}
}
else
{
wavecountdown -= Time.deltaTime;
}
}
void BeginNewRound()
{
state = spawnstate.COUTING;
wavecountdown = timebetweenwaves;
if (nextwave + 1>waves.Length - 1)
{
nextwave = 0;
}
else
{
nextwave++;
}
}
bool EnemyisAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Enemy") == null)
{
return false;
}
}
return true;
}
IEnumerator spawnwave(Wave _wave)
{
state = spawnstate.SPAWNING;
for (int i=0; i< _wave.count; i++)
{
spawnenemy(_wave.enemy); //error here!!!!
yield return new WaitForSeconds(_wave.delay);
}
state = spawnstate.WAITING;
yield break;
}
//the enemies spawn in random allocated spawnpoints
void spawnenemy(Transform _enemy)
{
Transform _sp = Spawnpoints[Random.Range(0, Spawnpoints.Length)];
Instantiate(_enemy, _sp.position, _sp.rotation);
}
}