- Home /
Strange ArgumentExceptionOutOfRange Error
I've written a script and in the script its looking up nodes sorted by rows and columns to find the adjacent nodes of the current node. I've put in checks to make sure that an ArgumentExceptionIsOutOfRange error doesn't happen, and its never happened before, but now it is. I created two variables: row
and column
, which is the row/column of the current node. Then when searching for the nodes, I created a check to make sure that it wasn't searching for nodes that weren't there:
if(column > 0){
if(row < numberOfRows){
if(column < numberOfColumns){
if(column > 0 && row < numberOfRows){
etc....
But what's happening is that the out of range error is occurring on these lines:
if(row < numberOfRows){
Node down = nodes[column].nodes[row+1];
if(down.node.gameObject.layer == 11 && !closedTransforms.Contains(down.node)){
and:
if(column < numberOfColumns){
Node downRight = nodes[column+1].nodes[row+1];
if(downRight.node.gameObject.layer == 11 && !closedTransforms.Contains(downRight.node)){
(Note: the second block of code is under the if(row < numberOfRows){
statement).
It shouldn't be giving me an error because it checks to see if the row is less then the number of rows, which means if the number of rows is 10, the rest of the code will only execute if the row is 9, 8, 7..... which means that when I do [row+1]
, the biggest number it could give me is 10....which is the number of rows, which shouldn't be a problem. There are many other statements like this and none of them are giving me errors except for these two. So I ran debugs to check to see if the number of nodes in each row was the same as the numberOfRows
variable and the same for numberOfColumns
and it all matches and up. Its sometimes giving me errors like this where it says the column is out of range when the column is searching for is 6 and the actual number of columns is 12. What could possibly be wrong?!?
Answer by Dave-Carlile · Dec 31, 2012 at 09:37 PM
Arrays are 0 based, which means an array with 10 elements has indexes from 0 to 9. So, in your example, when row + 1 = 10 then you are indeed trying to access an invalid array element.
Oh yes, thats right, I didn't take that into consideration. Thanks!