- Home /
Index Out of Range Exception - can't figure it out
The error is being thrown up in Start() on Line 2
timeRemaining = timeUntilNextAttack[k];
I can't figure this one out.... the index is 10 floats long, assigned in the inspector.
using System.Collections;
public class GameManager : MonoBehaviour {
//Timer Variables
public float[] timeUntilNextAttack;
private float timeRemaining;
public UILabel timerCountdown;
private int days, hours, minutes, seconds, currentAttack;
private string daytext, hourtext, minutetext, secondtext;
public GameObject airEnemies, groundEnemies;
//SpawnPoints
public Transform[] northGroundSpawns, eastGroundSpawns, westGroundSpawns, southGroundSpawns;
public Transform[] northAirSpawns, eastAirSpawns, westAirSpawns, southAirSpawns;
public Transform[] caveSpawns;
//Attack Wave Specific Variables
public int[] attack1WaveNumber;
public int[] attack1EnemyNumber;
public GameObject[] attack1EnemyType;
public string[] attack1EnemyDirection;
public int[] attack1EnemyLevel;
//begins initial timer at start of game.
//Subsequent timers set at end of attack
public void Start(){
int k = 0;
timeRemaining = timeUntilNextAttack[k];
SetAttackTimer();
}
//starts timer
public void SetAttackTimer(){
InvokeRepeating ("Countdown", 1.0f, 1.0f);
}
//actual timer and relay of information to in-game text displaying timer
public void Countdown(){
Debug.Log (timeRemaining+" second remaining");
timeRemaining -= 1f;
days = Mathf.FloorToInt(timeRemaining / 86400F);
hours = Mathf.FloorToInt((timeRemaining - (days*86400)) / 3600F);
minutes = Mathf.FloorToInt((timeRemaining - (days*86400)-(hours*3600)) / 60F);
seconds = Mathf.FloorToInt(timeRemaining - (days*86400)-(hours*3600)-(minutes*60));
if(days > 1)daytext = " days, ";
else if(days == 1)daytext = " day, ";
else if(days < 1){
days = 0;
daytext = "";
}
if(hours > 1)hourtext = " hours, ";
else if(hours == 1)hourtext = " hour, ";
else if(hours < 1){
if(days < 1){
hours = 0;
hourtext = " ";
}
else{
hourtext = " hours, ";
}
}
if(minutes > 1)minutetext = " mins, ";
else if(minutes == 1)minutetext = " min, ";
else if(minutes < 1){
if(hours < 1 || days < 1){
minutes = 0;
minutetext = " ";
}
else{
minutetext = " mins, ";
}
}
if(seconds > 1)secondtext = " secs";
else if(seconds == 1)secondtext = " sec";
else if(seconds < 1){
if(hours < 1 || days < 1 || minutes < 1){
seconds = 0;
secondtext = " ";
}
else{
secondtext = " secs";
}
}
timerCountdown.text = days+daytext+hours+hourtext+minutes+minutetext+seconds+secondtext;
//timer is up, start the attack and cancel the timer
if(timeRemaining <=0){
StartAttack();
CancelInvoke("Countdown");
}
}
//initiates the oncoming attack
public void StartAttack(){
Debug.Log ("Start Attack!");
if(currentAttack == 0)Attack1();
}
//Attack # 1
public void Attack1(){
//find max wave number and replace timer with wave of wave count
int maxWave = 0;
for (int i = 0; i < attack1WaveNumber.Length; i++)
{
if (attack1WaveNumber[i] > maxWave)
{
maxWave = attack1WaveNumber[i];
}}
timerCountdown.text = "Wave 1 of "+maxWave;
Debug.Log (maxWave+" Is the max wave");
//for each wave # in Attack#Wave Number...instantiate guys from other arrays
for (int j = 0; j < attack1WaveNumber.Length; j++)
{
if (attack1WaveNumber[j] == 1)
{
if(attack1EnemyDirection[j] == "north"){
if(attack1EnemyType[j].tag == "groundenemy")
{ int spawnChoice = Random.Range(0,northGroundSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], northGroundSpawns[spawnChoice].position, northGroundSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = groundEnemies.transform;
}
}
else if(attack1EnemyType[j].tag == "airenemy")
{ int spawnChoice = Random.Range(0,northAirSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], northAirSpawns[spawnChoice].position, northAirSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = airEnemies.transform;
}
}
}
else if(attack1EnemyDirection[j] == "east"){
if(attack1EnemyType[j].tag == "groundenemy")
{ int spawnChoice = Random.Range(0,eastGroundSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], eastGroundSpawns[spawnChoice].position, eastGroundSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = groundEnemies.transform;
}
}
else if(attack1EnemyType[j].tag == "airenemy")
{ int spawnChoice = Random.Range(0,eastAirSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], eastAirSpawns[spawnChoice].position, eastAirSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = airEnemies.transform;
}
}
}
if(attack1EnemyDirection[j] == "west"){
if(attack1EnemyType[j].tag == "groundenemy")
{ int spawnChoice = Random.Range(0,westGroundSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], westGroundSpawns[spawnChoice].position, westGroundSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = groundEnemies.transform;
}
}
else if(attack1EnemyType[j].tag == "airenemy")
{ int spawnChoice = Random.Range(0,westAirSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], westAirSpawns[spawnChoice].position, westAirSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = airEnemies.transform;
}
}
}
if(attack1EnemyDirection[j] == "south"){
if(attack1EnemyType[j].tag == "groundenemy")
{ int spawnChoice = Random.Range(0,southGroundSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], southGroundSpawns[spawnChoice].position, southGroundSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = groundEnemies.transform;
}
}
else if(attack1EnemyType[j].tag == "airenemy")
{ int spawnChoice = Random.Range(0,southAirSpawns.Length);
//spawn number of enemy above at place above
int numberToSpawn = attack1EnemyNumber[j];
for (int k = 0; k < numberToSpawn; k++) {
GameObject newEnemy = Instantiate(attack1EnemyType[j], southAirSpawns[spawnChoice].position, southAirSpawns[spawnChoice].rotation) as GameObject;
Debug.Log (newEnemy.name);
newEnemy.GetComponent<Enemy>().unitLevel = attack1EnemyLevel[j];
newEnemy.transform.parent = airEnemies.transform;
}
}
}
}
}
}
public void AttackWin(){
//setup next timer
currentAttack += 1;
int i = currentAttack;
//timeRemaining = timeUntilNextAttack[i];
SetAttackTimer();
}
}
Answer by clunk47 · Aug 30, 2013 at 01:43 AM
Are you filling in the values of timeUntilNextattack in the inspector? You could fill your array in Start() like so for testing:
timeUntilNextAttack = new float[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
First off, thanks so much for the fast response - wasn't expecting anything until tomorrow!
When I put this line of code directly above the problem in Start(), it eli$$anonymous$$ates that issue and then creates a new error at line 65.
Not sure what that means about the original issue?
NullReferenceException: Object reference not set to an instance of an object
Game$$anonymous$$anager.Countdown()(at Assets/Resources/Game$$anonymous$$anager.cs:173)
You're adding ints to a string without converting ToString(). That may be causing an issue, but as far as the null issue goes, make sure NOTHING is null, EVERYTHING has been assigned a value. days, dayText, hours, hourText, and so on.
timerCountdown.text =
days.ToString()
+daytext
+hours.ToString()
+hourtext
+$$anonymous$$utes.ToString()
+$$anonymous$$utetext
+seconds.ToString()
+secondtext;
Have you initialized timerCountdown or is it unassigned? In my opinion this is the case of exception.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Array index help. 1 Answer
Convert To Using Array 2 Answers
Help using LitJson 1 Answer
How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers