- Home /
Index Out of Range Exception?
//conditions to check
public int[] cause =
{ 0 //Enemy is at full health
, 1 //Enemy health <= 90%
, 2 //Enemy health <= 75%
, 3 //Enemy health <= 50%
, 4 //Enemy ehalth <= 25%
, 5 //Owner is at full health
, 6 //Owner health <= 90%
, 7 //Owner health <= 75%
, 8 //Owner health <= 50%
, 9 //Owner health <= 25%
, 10 //DEFAULT
};
//actions to perform
public int[] effect =
{ 0 //Light Attack
, 1 //Medium Attack
, 2 //Heavy Attack
, 3 //Magic: Fire
, 4 //Magic: Ice
, 5 //Magic: Cure
, 6 //Magic: Cura
, 7 //Use item
, 8 //Unsummon
, 9 //UNASSIGNED
, 10 //UNNASSIGNED
};
// Use this for initialization
void Start () {
options = new int[][,] //first thrown here.
{
new int[,] { { cause[9],effect[6] }, { cause[8],effect[6] }, { cause[7],effect[5] }, { cause[4],effect[2] }, { cause[3],effect[3] }, { cause[10],effect[6] }, { cause[2],effect[7] }, { cause[2],effect[8] }, { cause[1],effect[9] }, { cause[0],effect[0] }}
};
}
The code above is a snippet that is giving me a slight issue. with the variables as is, it throws an ArrayIndexOutofRange error at the line indicated with a comment, and it throws a second wherever i am calling the data. at the moment i am using a test calling of:
Debug.Log(options[0][0,0]); //should return 9
here's the interesting part, if i change the 6th cause of options from cause[10] to anything else that i have listed, then i don't get the error.
I am not getting any errors when I try your code. Since your 'cause' and 'effect' variables are public members of a $$anonymous$$onoBehaviour, is it possible that you have accidentally changes the serialized values of these arrays from the inspector?
Try adding [System.NonSerialized] just above each
[System.NonSerialized]
public int[] cause =
{ 0 //Enemy is at full health
, 1 //Enemy health <= 90%
, 2 //Enemy health <= 75%
, 3 //Enemy health <= 50%
, 4 //Enemy ehalth <= 25%
, 5 //Owner is at full health
, 6 //Owner health <= 90%
, 7 //Owner health <= 75%
, 8 //Owner health <= 50%
, 9 //Owner health <= 25%
, 10 //DEFAULT
};
//actions to perform
[System.NonSerialized]
public int[] effect =
{ 0 //Light Attack
, 1 //$$anonymous$$edium Attack
, 2 //Heavy Attack
, 3 //$$anonymous$$agic: Fire
, 4 //$$anonymous$$agic: Ice
, 5 //$$anonymous$$agic: Cure
, 6 //$$anonymous$$agic: Cura
, 7 //Use item
, 8 //Unsummon
, 9 //UNASSIGNED
, 10 //UNNASSIGNED
};
Answer by Ashokkumar-M · Aug 10, 2017 at 06:45 AM
Hi, @WhipJr I checked your code. Its working fine. The Only problem I could figure out in your case is if you have changed cause array in the inspector by mistake this problem might occur. So check the inspector whether you have 11 elements in cause and effect array and if not change it. Alternatively change the cause and effect array to private so the values will not be changed in inspector.
int[] cause =
{ 0 //Enemy is at full health
, 1 //Enemy health <= 90%
, 2 //Enemy health <= 75%
, 3 //Enemy health <= 50%
, 4 //Enemy ehalth <= 25%
, 5 //Owner is at full health
, 6 //Owner health <= 90%
, 7 //Owner health <= 75%
, 8 //Owner health <= 50%
, 9 //Owner health <= 25%
, 10 //DEFAULT
};
//actions to perform
int[] effect =
{ 0 //Light Attack
, 1 //Medium Attack
, 2 //Heavy Attack
, 3 //Magic: Fire
, 4 //Magic: Ice
, 5 //Magic: Cure
, 6 //Magic: Cura
, 7 //Use item
, 8 //Unsummon
, 9 //UNASSIGNED
, 10 //UNNASSIGNED
};
// Use this for initialization
void Start()
{
int[][,] options = new int[][,] //first thrown here.
{
new int[,] { { cause[9],effect[6] }, { cause[8],effect[6] }, { cause[7],effect[5] }, { cause[4],effect[2] }, { cause[3],effect[3] }, { cause[10],effect[6] }, { cause[2],effect[7] }, { cause[2],effect[8] }, { cause[1],effect[9] }, { cause[0],effect[0] }}
};
Debug.Log(options[0][0, 0]);
}
This is the code I checked. Its working fine.
thank you. i must have been too tired and not noticed that the editor wasn't updating the array when i added more too it. setting it to private was the best options since there is no good reason to need to edit that outside of the script
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
IndexOutOfRangeException help on debug 3 Answers
Set default length for an array of elements of a custom class in inspector 0 Answers