Hello,
i am working on Tower Defense game. I am getting this error since yesterday and i am still don’t know how to solve this.
Error:
IndexOutOfRangeException: Index was outside the bounds of the array.
WaveSpawner+<SpawnWave>d__11.MoveNext () (at Assets/Scripts/WaveSpawner.cs:56)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <c6b52566f59b49fc861a7812a1ea2f6b>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
WaveSpawner:Update() (at Assets/Scripts/WaveSpawner.cs:41)
Wave Spawner Script:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour
{
public static int enemiesAlive = 0;
public Wave[] waves;
public float timeBetweenWaves;
public Transform spawnpoint;
public Text countdownText;
public GameObject countdownPanel;
public GameManager gameManager;
private float countdown = 3.5f;
private int waveIndex = 0;
private void Start()
{
countdownPanel.SetActive(true);
enemiesAlive = 0;
}
// Update is called once per frame
void Update()
{
if (enemiesAlive > 0)
{
return;
}
if (waveIndex == waves.Length)
{
gameManager.WinLevel();
}
if (countdown <= 0f)
{
countdownPanel.SetActive(false);
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
countdownText.text = countdown.ToString("0");
//Mathf.Round(PlayerStats.waves).ToString("00")
}
IEnumerator SpawnWave()
{
PlayerStats.waves++;
Wave wave = waves[waveIndex]; //IndexOutOfRangeException
//enemiesAlive = wave.enemyCount;
for (int i = 0; i < wave.enemy1Count; i++)
{
SpawnEnemy1(wave.enemy1);
yield return new WaitForSeconds(1f / wave.enemy1SpawnTime);
}
for (int i = 0; i < wave.enemy2Count; i++)
{
SpawnEnemy2(wave.enemy2);
yield return new WaitForSeconds(1f / wave.enemy2SpawnTime);
}
waveIndex++;
}
void SpawnEnemy1(GameObject enemy1)
{
Instantiate(enemy1, spawnpoint.position, spawnpoint.rotation);
enemiesAlive++;
}
void SpawnEnemy2(GameObject enemy2)
{
Instantiate(enemy2, spawnpoint.position, spawnpoint.rotation);
enemiesAlive++;
}
}