- Home /
Index Array is out of Range
Hey so i'm making a sort of puzzle game in which you need to match tiles in groups of three. I have a function checking to see if the type of fruit in each tile is the same in the coordinate one over to the right of the tile, and two over to the right. It works fine in the game, but every time I move a piece (this function is called every time you move a piece btw) i get an Index Out of Range exeption error. Im not sure why, since Im certain im only checking on tiles that are in the bounds of the grid.
void CheckGrid()
{
foreach (Tile tile in tiles)
{
if (tile.fruitType != Tile.FruitType.nothing)
{
if (tile.tileCoord.x < gridSize - 1)
{
if (tile.fruitType == tiles[tile.tileCoord.x + 1, tile.tileCoord.y].fruitType && tile.fruitType == tiles[tile.tileCoord.x + 2, tile.tileCoord.y].fruitType)
{
tile.fruitType = Tile.FruitType.nothing;
tiles[tile.tileCoord.x + 1, tile.tileCoord.y].fruitType = Tile.FruitType.nothing;
tiles[tile.tileCoord.x + 2, tile.tileCoord.y].fruitType = Tile.FruitType.nothing;
}
}
}
}
}
Answer by NoCandyIncluded · Oct 18, 2019 at 09:08 PM
I would assume that one of the 'tiles[]' statements such as 'tiles[tile.tileCoord.x + 1, tile.tileCoord.y].fruitType', doesnt exist. So when you are adding 1 to tile.tileCoord.x, it is likely getting a coord outside of the grid. you should check for if the tile you are getting in the tiles[] array exists first, before executing or checking if something is equal to it. edit: also, 'tile.fruitType != Tile.FruitType.nothing' doesnt check if other tiles are in the grid, it only checks if the single tile is on the grid, if this is what you wanted it to do.
Hey, I appreciate the help.
I don't understand how it could be referencing a tile that isn't in the rage. I first check if it even has fruit in it, and it should be impossible for there to be any tiles without fruit in them. It then checks to see whether it should even check for a match by saying that the first tile in the check needs to at least 2 from the edge of the grid, meaning when it adds x + 1 and 2, it should only be possible for to reference tiles within the array.
I tried checking if those two tiles were null beforehand and it hasn't seemed to help so far.
If it helps, it assign the tiles to the grid as soon as the grid is generated, starting in the bottom left corner and moving right until it hits the max size and then moves up a y and resets back to 0 on the x. So maybe it has something to do with that?
This condition will already cause an index out of bounds exception if the tile you are currently checking has a tileCoord that is at the right most edge:
tile.fruitType == tiles[tile.tileCoord.x + 2, tile.tileCoord.y].fruitType
Just keep in $$anonymous$$d that you iterate over all tiles in your tiles array. So your loop will hit every one of them. So it will also go over the right most tile. However you take the coordinate of that right most tile and add 2 to the x coordinate which will result in an index that doesn't exist.
If "gridSize" is the actual horizontal size of your grid you probably wanted to use:
if (tile.tileCoord.x < gridSize - 2)
ins$$anonymous$$d of
if (tile.tileCoord.x < gridSize - 1)
That seems to have fixed it, thank you. I had my math off it seems.
Your answer
Follow this Question
Related Questions
IndexOutofRangeException 1 Answer
IndexOutOfRangeException: Array index is out of range. 0 Answers
Mysterious IndexOutOfRangeException 1 Answer
IndexOutOfRangeException in build but not editor 0 Answers
FindGameobjectsWithTag -> Array 2 Answers