- Home /
Wierd IndexOutOfRangeException : Array index is out of range
It's giving me a weird IndexOutOfRangeException
here is my code
int moveRow = 1;
int moveCol = 1;
int sum1 = 0;
int sum2 = 0;
int rowIndex1 = 1;
int rowIndex2 = 0;
int firstTableRow = 1;
int firstTableCol = 1;
int secondTableRow = 1;
int secondTableCol = 0;
int thirdTableRow = 0;
int thirdTableCol = 1;
int fourthTableRow = 0;
int fourthTableCol = 0;
int counterNextRow = 0;
for (int col = 0; col < table.GetLength(0); col++)
{
GameObject p = Instantiate(big_eye_gameobject) as GameObject;
p.transform.SetParent(pos_big_eye_road);
p.transform.localScale = Vector3.one;
img = (RawImage)p.GetComponent<RawImage>();
for (int row = 0; row < table.GetLength(1); row++)
{
//if not equal length BLUE[0] else RED[1]
//moverow = 1, movecol = 1 default
if (table[moveCol, moveRow] == null)
//1,1
{
//must be on table[2,0]
//scan
//3,2
string col1 = table[rowIndex1, row];
string col0 = table[rowIndex2, row];
if (col1 != null)
{
sum1++;
}
if (col0 != null)
{
sum2++;
}
counterNextRow += 1;
}
else
{
//if not null and on the first process
if (counterNextRow == 0)
{
//r1 = table[1,1] r2 = table[1,0] r3 = table[0,1] r4 = table[0,0]
string d1 = table[firstTableRow, firstTableCol];
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
if (d1 != null && d2 != null && d3 != null && d4 != null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
moveRow += 1;
}
//hindi unang process
else
{
if (moveRow > 1)
{
firstTableCol += 1;
secondTableCol += 1;
thirdTableCol += 1;
fourthTableCol += 1;
//r1 = table[2,1] r2 = table[2,0] r3 = table[1,1] r4 = table[1,0]
string d1 = table[firstTableRow, firstTableCol];
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
if (d1 != null && d2 != null && d3 != null && d4 != null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else if (d1 != null && d2 != null && d3 == null && d4 == null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
moveRow += 1;
}
else
{
firstTableRow += 1;
secondTableRow += 1;
thirdTableRow += 1;
fourthTableRow += 1;
//r1 = table[2,1] r2 = table[2,0] r3 = table[1,1] r4 = table[1,0]
string d1 = table[firstTableRow, firstTableCol];
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
if (d1 != null && d2 != null && d3 != null && d4 != null)
{
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
moveRow += 1;
}
}
}
}
//condition that if 1st board column 1 and column 0 has an equal length
//mark on the 2nd board
if (sum1 == sum2)
{
//red[1]
img.texture = (Texture)NewTexture[1];
p.SetActive(true);
}
else
{
//blue[0]
img.texture = (Texture)NewTexture[0];
p.SetActive(true);
}
//increment everytime the column is finish scanning
sum1 = 0;
sum2 = 0;
rowIndex1++;
rowIndex2++;
moveCol++;
moveRow = 1;
// //get rid of exception
// if(moveCol < table.GetLength(0)){
// break;
// }
p.transform.localPosition = new Vector3(bigEyeXIndex * 7, bigEyeXIndex * -8, 0f);
}
On this part is giving me and Array Index is out of range
if (moveRow > 1)
{
firstTableCol += 1;
secondTableCol += 1;
thirdTableCol += 1;
fourthTableCol += 1;
//r1 = table[2,1] r2 = table[2,0] r3 = table[1,1] r4 = table[1,0]
string d1 = table[firstTableRow, firstTableCol]; // <------------ here is index array out of range
string d2 = table[secondTableRow, secondTableCol];
string d3 = table[thirdTableRow, thirdTableCol];
string d4 = table[fourthTableRow, fourthTableCol];
firstTableCol is out of range because you +=1. firstTableCol suppose tobe 0 but it was 1. Debug that firstTableCol and see if it actually go more than 3 (0,1,2,3 and 4 is out range). Array start at 0 not at 1
i tried debugging it and it gives me this
Debug.Logs on every tables[table[firstTableRow, firstTableCol]; i have so here are result : Values of tables Cols : 1010, Values of tables Rows: 1100, Values of tables Cols3: 1010, Values of tablesRows3 :2211, Values of tablesCols2 :2121, Values of tablesRows2 :2211, Values of tablesCols2 :3232, Values of tablesRows2 :2211, Values of tablesCols2 :4343, Values of tablesRows2 :2211, Values of tablesCols3 :4343,Values of tablesRows3 :3322,Values of tablesCols2 :5454,Values of tablesRows2 :3322
then IndexOutOfRangeException
This is unreadable. But my guess is table rows 4 and column 4 is out of range? and "Values of tablesCols2 :5454" is little weird
Answer by Ginxx009 · May 28, 2018 at 03:32 AM
The problem with my code is that I am not checking its variables value first on the 2D array so here's what I did
moveCol + 1 < table.GetLength(0)
Just lie that .
Your answer
![](https://koobas.hobune.stream/wayback/20220612163401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Class Array referencing 1 Answer
Multiple Cars not working 1 Answer
Count every row and column of multidemensional array (c# unity) 1 Answer
Distribute terrain in zones 3 Answers