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 Ultronium · Jul 25, 2017 at 08:22 AM · c#arrayindexoutofrangeexceptionarray-out-of-range-except

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! :)

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
0

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

Comment
Add comment · Show 10 · 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 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 < 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.

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

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

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

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.

avatar image 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 + 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.

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

Somewhere you are comparing Tile to an integer

LineOfTiles[i] == integer

you should change it to .Number == integer

avatar image 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 < 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.

avatar image Ultronium Bunny83 · Jul 25, 2017 at 11:48 AM 0
Share

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 :( alt text

screen-shot-2017-07-25-at-124048-pm.png (22.4 kB)
avatar image ShadyProductions Ultronium · Jul 25, 2017 at 11:51 AM 0
Share

Somewhere you are trying to access an index higher than + 2, it's not in that piece of code you just shared with us.

Show more comments

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

362 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 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 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

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


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