- Home /
Spawning more than one type of enemy script
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. https://www.youtube.com/watch?v=Vrld13ypX_I&list=PLPV2KyIb3jR42oVBU6K2DIL6Y22Ry9J1c∈dex=25
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);
}
}
Answer by TheyLeftMe4Dead · Feb 24, 2017 at 04:19 AM
Each enemy group is made of enemies with certain characteristics (Or at least I assume). Since each enemy group has multiple characteristics, it can't be simply be stored as say, an integer or String. It must be stored in a class or struct. You would need your enemySpawner class to read through an array of enemy groups, then read through each enemyGroup object. For each enemyGroup object, you would need to spawn the enemyCount & enemyType defined by that enemyGroup.
Introtoprogramming.info has some great information on well, the foundations of programming. It might be a good resource for you. You can also check out Brakeys on youtube, who has a tower defense game series (He has a similar problem that he solves).
Answer by DrewWasTaken · Jan 11, 2020 at 06:08 PM
I'm still looking for an answer to this, it's surprisingly hard to find one anywhere...
Answer by Huntsman034 · Nov 19, 2020 at 03:28 PM
@DrewWasTaken Did you Find an answer?
Someone helped me through it and essentially rewrote and improved my code. It was a while ago but I seem to remember it was just passing through arrays and instantiating them?