Quantcast
Channel: Latest Questions by Garndaddy
Viewing all articles
Browse latest Browse all 8

Array index is out of range

0
0
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); }

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images