- Home /
Array index is out of range
I've been following the Tower Defense tutorials on cgcookie.com and am in the process of creating the waves for the game. I'm getting the "array index is out of range error" when I start the game. The error occurs in the SpawnNewEnemy function at the bottom of the script. I have read a lot of similar questions on Unity Answers but none of them have solved the problem for me. I went ahead and posted the entire script just in case I messed up somewhere else. Any ideas on what I'm doing wrong?
pragma strict
static var playerDamage = 0;
var waveActive : boolean = false;
var spawnEnemies : boolean = false;
var healthCount : int = 100;
var scoreCount : int = 0;
var moneyCount : int = 0;
var waveText : UILabel;
var scoreText : UILabel;
var moneyText : UILabel;
var healthText : UILabel;
var waveLevel : int = 0;
var difficultyMultiplier : float = 1.0;
var waveLength : float = 20.0;
var intermissionTime : float = 45.0;
private var waveEndTime : float = 0;
var enemyPrefabs : GameObject[];
var shooterSpawns : Transform;
private var shooterSpawnPoints : Transform[];
var respawnMinBase : float = 3.0;
var respawnMaxBase : float = 10.0;
private var respawnMin : float = 3.0;
private var respawnMax : float = 10.0;
var respawnInterval : float = 2.5;
var enemyCount : int = 0;
private var lastSpawnTime : float = 0;
function Start () {
shooterSpawnPoints = new Transform[shooterSpawns.childCount];
var i : int = 0;
for( var theSpawnPoint : Transform in shooterSpawns) {
shooterSpawnPoints[i] = theSpawnPoint;
i++;
}
SetNextWave();
StartNewWave();
}
function Update () {
if(waveActive) {
if(Time.time >= waveEndTime) {
spawnEnemies = false;
if(enemyCount ==0) {
FinishWave();
}
}
if(spawnEnemies) {
if(Time.time > (lastSpawnTime + respawnInterval)) {
SpawnNewEnemy();
}
}
}
}
function UpdateHUD() {
waveText.text = "Wave: " + waveLevel;
scoreText.text = "Score: " + scoreCount;
healthText.text = "Health: " + healthCount;
moneyText.text = "Money: " + moneyCount;
}
function FinishWave() {
waveActive = false;
yield WaitForSeconds(intermissionTime);
SetNextWave();
StartNewWave();
}
function SetNextWave() {
waveLevel++;
difficultyMultiplier = ((Mathf.Pow(waveLevel,2)) * .005) + 1;
respawnMin = respawnMinBase * (1/difficultyMultiplier);
respawnMax = respawnMaxBase * (1/difficultyMultiplier);
}
function StartNewWave() {
UpdateHUD();
SpawnNewEnemy();
waveEndTime = Time.time + waveLength;
waveActive = true;
spawnEnemies = true;
}
function SpawnNewEnemy() {
var enemyChoice = Random.Range(0,enemyPrefabs.length);
var spawnChoice : int;
if(enemyPrefabs[enemyChoice].tag == "Shooter Enemy") {
spawnChoice = Random.Range(0, shooterSpawnPoints.length);
Instantiate(enemyPrefabs[enemyChoice], shooterSpawnPoints[spawnChoice].position, shooterSpawnPoints[spawnChoice].rotation);
}
else {
//add other enemy types here
}
enemyCount++;
lastSpawnTime = Time.time;
respawnInterval = Random.Range(respawnMin, respawnMax);
}
Answer by robertbu · Feb 28, 2014 at 12:59 AM
It is always helpful to folks attempting to answer your question if you can give direct them to the line that is giving the error. Usually you can do that by copying and pasting the error message from the console into the question.
I'm guessing the error is on line 111. This will happen if the 'enemyPrefabs' array has a length of 0. It appears that this array is to be initialized by drag and drop in the inspector. If you find that the array is initialized, check to make sure this script is not attached to more than one game object.
Here is the error message:
IndexOutOfRangeException: Array index is out of range. Level$$anonymous$$aster.SpawnNewEnemy () (at Assets/$$anonymous$$y Scripts/Level$$anonymous$$aster.js:113)
I checked all the gameobjects in my scene and the ones that will be instantiated and none of them have the script attached. It says line 113 in the error message which is this line:
Instantiate(enemyPrefabs[enemyChoice], shooterSpawnPoints[spawnChoice].position, shooterSpawnPoints[spawnChoice].rotation);
So I was right about the line of the error. The issue is the initialization of the enemyPrefab array. Did you check the array in the Inspector to make sure it is initialized with enemy prefabs?
I did. I also changed the amount of prefabs I had in the array just to make sure it wasn't because of the random range or something. I might just copy the whole script to a new one and reattach it to my game object.
I tried putting a different prefab in the array with a different tag and the error did not appear. When I changed the tag to the one in the script is when the error appears. Don't know if this will help but I thought it was weird because nothing instantiates but the error disappears.
Looking deeper, there is another potential for your error. 'shooterSpawns' needs to be initialized. In particular, it appears that 'shooterSpawns' should be a parent object with a number of child objects at the positions where you want to span objects. Is this how you have your game setup? Did you initialize 'shooterSpawns' and if so, does it have children that are at the spawn point locations?