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.

You are watching: Indexoutofrangeexception: array index is out of range.

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; ns 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 .Number == 0 && LineOfTiles .Number != 0) to this:

if (LineOfTiles .Number == 0 && LineOfTiles .Number != 0) but it didn"t work..

Thanks for taking out the time to help me out! :)


Comment
Add comment
10 |3000 characters needed characters left characters surpassed
▼ Viewable by all users

1 Reply

· Add your reply Sort:
*

0

Answer by ShadyProductions ·Jul 25, 2017 at 08:26 AM


LineOfTiles 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 .Number == 0 && LineOfTiles.Length > i + 1 && LineOfTiles .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.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


Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼ Viewable by all users
*
Ultronium · Jul 25, 2017 at 08:36 AM 0
Share

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

Also, If I remove

LineOfTiles .Number == 22 /green/ &&

my error is gone because this uses and is causing the problem.


*
ShadyProductions Ultronium · Jul 25, 2017 at 08:39 AM 0
Share

Also my bad in the && LineOfTiles != 0I forgot to add .Number

should be && LineOfTiles .Number != 0

with my fix you can make

for (int i = 0; i become

for (int i = 0; i Just don"t forget to check for the index length, that you"re not exceeding it.

Accessing indexes using etc is a bad way of programming anyway..


*
Ultronium ShadyProductions · Jul 25, 2017 at 09:09 AM 0
Share

I included .Number and removed the "- 1" from

for (int i = 0; i but i"m getting the exact same error again.


*
Ultronium · Jul 25, 2017 at 08:39 AM 0
Share

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"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.


*
ShadyProductions Ultronium · Jul 25, 2017 at 09:18 AM 0
Share

Somewhere you are comparing Tile to an integer

LineOfTiles == integer

you should change it to .Number == integer


*
Bunny83 · Jul 25, 2017 at 11:22 AM 0
Share

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 So his code works just fine as long as he only uses in his code. However the actual problem is that us also uses which of course would fail for the last interation.

The for loop limit of i 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 ns --> 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 .

See more: Boil Em Mash Em Stick Em In A Stew Quote, Boil Em Mash Em

So just changing the for limit to ns 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 determine what this code is actually about and how that array structure is layouted / used.