- Home /
IndexOutOfRangeException: Array index is out of range.
Hey all! So halfway into development of this game, I got this error that I just can't solve. I'm super new to C# as well so I might have written some unnecessary super long codes that could have been done in a single line. I'm getting there.. hopefully.
So the error "IndexOutOfRangeException: Array index is out of range." occurs at the line where I've written "ERROR HERE" (This is just for pointing out the location of the error is not actually in the code).
bool MakeOneMoveDownIndex(Tile[] LineOfTiles)
{
for (int i = 0; i < LineOfTiles.Length - 1; i++)
{
//MOVE BLOCK
if (LineOfTiles [i].Number == 0 && LineOfTiles [i + 1].Number != 0)
{
LineOfTiles [i].Number = LineOfTiles [i + 1].Number;
LineOfTiles [i + 1].Number = 0;
return true;
}
//Blue + Red + Green = White
else if (LineOfTiles [i].Number != 0 &&
LineOfTiles [i].Number == 33 /*blue*/ &&
ERROR HERE: LineOfTiles [i + 1].Number == 11 /*red*/ &&
LineOfTiles [i + 2].Number == 22 /*green*/ &&
LineOfTiles [i].mergedThisTurn == false && LineOfTiles [i + 1].mergedThisTurn == false && LineOfTiles [i + 2].mergedThisTurn == false)
{
LineOfTiles [i].Number = 77; /*white*/
LineOfTiles [i + 1].Number = 0;
LineOfTiles [i + 2].Number = 0;
LineOfTiles [i].mergedThisTurn = true;
//GameOver();
return true;
}
If you require any more code let me know because this error is in the 300s line. But I don't think other lines of code are relevant to this error because they aren't array related.
In the line after //MOVE BLOCK, I've tried changing this:
if (LineOfTiles [i].Number == 0 && LineOfTiles [i + 1].Number != 0)
to this:
if (LineOfTiles [i].Number == 0 && LineOfTiles [i + 2].Number != 0)
but it didn't work..
Thanks for taking out the time to help me out! :)
Answer by ShadyProductions · Jul 25, 2017 at 08:26 AM
LineOfTiles [i + 1]
This is your problem, if you hit the last index, and you do + 1 that index doesn't exist so you get the error.
you shoud do it like this:
if (LineOfTiles [i].Number == 0 && LineOfTiles.Length > i + 1 && LineOfTiles [i + 1].Number != 0)
But then you still have a chance that your last index can be 0 so you would have to make an extra check and do something in there:
if ((LineOfTiles.Length -1) == i && LineOfTiles[i].Number == 0) {
// last tile is 0
// do something
}
Note: you will have to check for LineOfTiles.Length > i + 1
everywhere where you use the i + 1
I'm afraid that didn't do the trick. Gives this error:
Assets/Scripts/Game$$anonymous$$anager.cs(101,82): error CS0019: Operator
!=' cannot be applied to >operands of type
Tile' and`int'
I think my error lies in here where Length - 1 isn't working:
for (int i = 0; i < LineOfTiles.Length - 1; i++)
Also, If I remove
LineOfTiles [i + 2].Number == 22 /green/ &&
my error is gone because this uses [i + 2] and is causing the problem.
Also my bad in the && LineOfTiles [i + 1] != 0
I forgot to add .Number
should be && LineOfTiles [i + 1].Number != 0
with my fix you can make
for (int i = 0; i < LineOfTiles.Length - 1; i++)
become
for (int i = 0; i < LineOfTiles.Length ; i++)
Just don't forget to check for the index length, that you're not exceeding it.
Accessing indexes using [i + 2] etc is a bad way of program$$anonymous$$g anyway..
I added .Number and removed the "- 1" from
for (int i = 0; i < LineOfTiles.Length - 1; i++)
but i'm getting the exact same error again.
If this helps, I followed a tutorial where 2 tiles were being merged. In my case, I need 3 tiles to be merged (Red + Blue + Green) so I added Green and [i + 2]. I'm pretty sure this created the problem but I can't find a way around it. I really need to merge 3 tiles in order for my game to work so unfortunately, removing green isn't an option for me.
Somewhere you are comparing Tile to an integer
LineOfTiles[i] == integer
you should change it to .Number == integer
Uhm sorry but an additional condition like this LineOfTiles.Length > i + 1
inside the if statement seems to be very confusing. I don't get why people often use the "reverse logic". Usually you want to check that a variable is in the desired range. So you usually check the variable against the limit and not the limit against the variable.
It would be very weird if at the entrance of a club you're asked if 18 is smaller or equal to your age...
Also your condition is just his for loop limit written in reverse:
LineOfTiles.Length > i + 1 == i < LineOfTiles.Length - 1
So his code works just fine as long as he only uses [i + 1]
in his code. However the actual problem is that us also uses [i+2]
which of course would fail for the last interation.
The for loop limit of i < LineOfTiles.Length - 1
will also prevent the whole loop from executing when the length is only 1. It has to be at least 2. "i" starts at 0 but when length is just "1" the condition would be i < (1-1)
--> 0 < 0
--> false so it doesn't do any iteration at all which is right since the for loop requires at least 2 elements due to the [i+1]
.
So just changing the for limit to i < LineOfTiles.Length - 2
will work for his code since his for loop body seems to require at least 3 elements (i, i+1 and i+2). So an array with 5 elements would perform 3 iterations (i == 0,1,2)
Though it's hard to deter$$anonymous$$e what this code is actually about and how that array structure is layouted / used.
Hey. $$anonymous$$y code now looks like
bool $$anonymous$$akeOne$$anonymous$$oveDownIndex(Tile[] LineOfTiles)
{
for (int i = 0; i < LineOfTiles.Length - 2; i++)
{
//$$anonymous$$OVE BLOC$$anonymous$$
if (LineOfTiles [i].Number == 0 && LineOfTiles [i + 1].Number != 0)
{
LineOfTiles [i].Number = LineOfTiles [i + 1].Number;
LineOfTiles [i + 1].Number = 0;
return true;
}
Still the same error :(
Somewhere you are trying to access an index higher than + 2, it's not in that piece of code you just shared with us.
Your answer
Follow this Question
Related Questions
Index out of Range Exception Error 2 Answers
Class Array referencing 1 Answer
Multiple Cars not working 1 Answer
IndexOutOfRangeException in build but not editor 0 Answers
Distribute terrain in zones 3 Answers