I am trying to modify this script to accept multiple types of enemies in one wave. So far I can select multiple enemies and assign prefabs to them but I get this error:
Assets/Scripts/WaveSpawner.cs(10,28): warning CS0649: Field WaveSpawner.enemies' is never assigned to, and will always have its default value
null’
and this error when I run the game:
NullReferenceException: Object reference not set to an instance of an object
WaveSpawner+c__Iterator0.MoveNext () (at Assets/Scripts/WaveSpawner.cs:81)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour {
public static int EnemiesAlive = 0;
public Wave[] waves;
private EnemyBlueprint enemies;
public Transform spawnPoint;
public float timeBetweenWaves = 5f;
public float countdown = 2f;
//public float timeBetweenEnemies = 0.5f;
public Text waveCountdownText;
public Text wavesText;
private int waveIndex = 0;
//private int enemyIndex = 0;
private void Update()
{
if(EnemiesAlive > 0)
{
return;
}
if(countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveCountdownText.text = string.Format("{0:00.00}", countdown);
wavesText.text = (waveIndex + 1) + "/" + waves.Length;
}
//IEnumerator SpawnWave()
//{
// //Debug.Log("Wave Incoming");
// Wave wave = waves[waveIndex];
// for (int i = 0; i < wave.count; i++)
// {
// SpawnEnemy(wave.enemy);
// yield return new WaitForSeconds(1f / wave.rate);
// }
// waveIndex++;
// PlayerStats.Rounds++;
// if(waveIndex == waves.Length)
// {
// Debug.Log("LEVEL WON!");
// this.enabled = false;
// }
//}
IEnumerator SpawnWave()
{
//Debug.Log("Wave Incoming");
Wave wave = waves[waveIndex];
//EnemyBlueprint eb = enemies[enemyIndex];
for (int a = 0; a < wave.waveCount; a++)
{
yield return new WaitForSeconds(1f / wave.waveRate);
for (int i = 0; i < enemies.enemyCount; i++)
{
SpawnEnemy(enemies.enemy);
yield return new WaitForSeconds(1f / enemies.enemyRate);
}
}
waveIndex++;
PlayerStats.Rounds++;
if (waveIndex == waves.Length)
{
Debug.Log("LEVEL WON!");
this.enabled = false;
}
}
void SpawnEnemy(GameObject enemy)
{
Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
EnemiesAlive++;
}
//void DestroyEnemyOnTargetReach()
//{
// Destroy(gameObject);
//}
}
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Wave {
public EnemyBlueprint[] enemyWave;
public int waveCount;
public float waveRate;
}
using UnityEngine;
[System.Serializable]
public class EnemyBlueprint {
public GameObject enemy;
public int enemyCount;
public float enemyRate;
}