hey guys i have a problem with my array : IndexOutOfRangeException: Array index is out of range.
i Defined int tuss_array[]=new int[24] how can i fix it?
here is my code:
//provide random number
void RandomTuss(){
tuss1 = Random.Range (tussmin1, tussmax1);
tuss2 = Random.Range (tussmin2, tussmax2);
tuss_result= "TuSS1:"+tuss1.ToString () + "TuSS2:"+tuss2.ToString ();
if (tuss1 == tuss2)
{
Debug.Log("Great!"+"num of toss1:"+" "+tuss1+"num of toss2:"+" "+tuss2);
}
Debug.Log (" toss1 is:"+" " + tuss1+ " toss2 is:"+" " + tuss2 );
//Checking tuss1 and tuss2 then make a decision x coin should move
for (int i = 0; i <tuss_array.Length; i++) {
if(tuss_array[i]>0)
{
int tuss_pair=i-tuss1+tuss2;
int white_Coin_Tuss1=i-tuss1;
int white_Coin_Tuss2=i-tuss2;
int white_Coin_TussPair=i-tuss_pair;
if(tuss_array[white_Coin_Tuss1] > -2);
{
Debug.Log (" the white Coins should move to these points(tuss1): " + white_Coin_Tuss1.ToString());
}
if(tuss_array[white_Coin_Tuss2] > -2);
{
Debug.Log (" the white Coins should move to these points(tuss2): " + white_Coin_Tuss2.ToString());
}
if(tuss_array[tuss_pair] > -2 );
{
Debug.Log (" the white Coins can should to these points(Pair): " + tuss_pair.ToString());
}
}
}
}
Comment
Hard to say exactly without knowing what kind of numbers you put into your arrays. But for example if tuss1 and tuss2 are greater than 0, these
int tuss_pair=i-tuss1+tuss2;
int white_Coin_Tuss1=i-tuss1;
int white_Coin_Tuss2=i-tuss2;
will be negative at least on the first loop in your for-loop causing you to reference
tuss_array[white_Coin_Tuss2]
with a negative index
Your answer

Follow this Question
Related Questions
Checking if a point is withing any of the colliders 1 Answer
Array index is out of range. How to solve? 1 Answer
Storing functions to use later 0 Answers
Loops when generating random string 1 Answer