Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ragefordragons · Oct 18, 2019 at 07:43 PM · arraygridindexoutofrangeexception

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;
                 }
             }
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ragefordragons · Oct 21, 2019 at 11:31 PM 0
Share

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?

avatar image Bunny83 ragefordragons · Oct 22, 2019 at 01:56 AM 1
Share

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)
avatar image ragefordragons Bunny83 · Oct 22, 2019 at 03:08 PM 0
Share

That seems to have fixed it, thank you. I had my math off it seems.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges