• 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
Question by JekasG · Oct 31, 2014 at 12:00 AM · c#loop2d arrayjagged

2D Jagged Array , Array index is out of range.

Hi , i have t$$anonymous$$s 2D Array w$$anonymous$$ch is giving me a Array index is out of range, problem. What is the cause of t$$anonymous$$s problem and how can i solve t$$anonymous$$s ?

     void Debugging_One() {
 
         for(int a = 0; a < numOfPlatforms; a = a + 1) {
             randomXPosSegments = new int[a][];
             randomXSizeSegments = randomXSize * 2;
             //Debug.Log(a);
             //Debug.Log(randomXSizeSegments);
             for(int b = 0; b < randomXSizeSegments; b = b + 1) {
                 // randomXPosSegments[a][b] = 0;
                 randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};
                 //Debug.Log(b);
             }
 
         }
 
     }




Comment

People who like this

0 Show 2
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 AlwaysSunny · Oct 30, 2014 at 11:22 PM 0
Share

What line throws the exception?

note that a=a+1 is equivalent to a++

avatar image JekasG · Oct 31, 2014 at 12:01 AM 0
Share
 IndexOutOfRangeException: Array index is out of range.
 (wrapper stelemref) object:stelemref (object,intptr,object)
 Level_Generator.Debugging_One () (at Assets/_Script/Level_Generator.cs:157)
 Level_Generator.Update () (at Assets/_Script/Level_Generator.cs:98)

 Level_Generator.cs:157

is

 randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Habitablaba · Oct 31, 2014 at 12:22 AM

Below I have annotated your code a bit.

 for(int a = 0; a < numOfPlatforms; a = a + 1) {
   // you are starting a at 0, so the first time through you are creating
   // a 2d array where the first size is 0 and the 2nd size is not$$anonymous$$ng...
   // the second time through it is 1, and the t$$anonymous$$rd it is 2.
   randomXPosSegments = new int[a][]; 

   randomXSizeSegments = randomXSize * 2;

   for(int b = 0; b < randomXSizeSegments; b = b + 1) {
     // ok, so now we're creating the 2nd part of the array
     // we are giving it a size of 1, and we're overwriting it
     // every iteration of the the inner loop.
     // at a = 0 (the first iteration), you are accessing the 0th
     // element of an array of size 0. T$$anonymous$$s will cause issues.
     randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};
   }
 }
Comment
Bunny83

People who like this

1 Show 0 · 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

Answer by dCalle · Jul 26, 2016 at 09:39 AM

I can't read anyt$$anonymous$$ng from that. but let me guess, right now it doesn't throw an error, right?^^

alright, let me rewrite it the way I t$$anonymous$$nk it is right^^

 for(int a = 0; a < numOfPlatforms; a++)
 {
     randomXSizeSegments = randomXSize* 2;
     // you forgot to initilialize the second part of the 2D Array
     randomXPosSegments = new int[a, randomXSizeSegments];
 
     for(int b = 0; b < randomXSizeSegments; b++)
     {
         randomXPosSegments[a, b] = new int[] {(int)(randomXSize - 0.5)};
     }
 }

Hopefully that solves the problem. otherwise, you really need to give more information. like in w$$anonymous$$ch line the error is thrown.

Comment

People who like this

0 Show 1 · 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 Bunny83 · Jul 26, 2016 at 10:33 AM 0
Share

Uhm, it does throw an error ^^. @Habitablaba kind of explained the problem about two years ago in his answer. He basically has the array-creating inside the loops and used the wrong size for the arrays. He used the current iteration index as size.

Your example doesn't use a Jagged array but a multidimensional array. Also you do the same thing wrong. You recreate and overwrite the array each "a"-iteration. You also pass the current index ("a") as size for the first dimension. Since a is "0" the first iteration your second loop would also throw an error since the index "0" isn't valid in an empty array.

If you use a multidimansional array it should be created once outside all for loops. Multidimensional arrays have a small limitation: They can only define a rectangle / cube / hypercube... Jagged arrays allows you to have different element counts in the nested arrays.

For example:

 int[][] array = new int[3][];
 array[0] = new int[2];
 array[1] = new int[5];
 array[2] = new int[3];

So element 0 of the outer array only has two sub elements while element 1 has 5. Graphically that array would look like that:

 [
     [0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0],
 ]

A multidim array would look like this:

int[,] array = new int[3,5];

 [
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
 ]

As you can see each "row" always have the same element count as it forms a rectangle

avatar image

Answer by Bunny83 · Jul 26, 2016 at 10:19 AM

Not sure why t$$anonymous$$s got bumped after so long, but since there's still no accepted answer, here's how you should do t$$anonymous$$s;

 void Debugging_One()
 {
     // create the outer array only "once" and give it the right size:
     randomXPosSegments = new int[numOfPlatforms][];
     for(int a = 0; a < numOfPlatforms; a++)
     {
         randomXSizeSegments = randomXSize * 2;
         // For each element in the outer array we have to create an inner array with the desired size:
         randomXPosSegments[a] = new int[randomXSizeSegments];
         for(int b = 0; b < randomXSizeSegments; b++)
         {
             // now do whatever you want to do with your elements
             randomXPosSegments[a][b] =  {(int)(randomXSize - 0.5)};;
         }
     }
 }

From the original code it's hard to tell what you actually want to do. At the moment we create an outer array with "numOfPlatforms" inner arrays. The inner arrays will have a length of "randomXSizeSegments". I just deduced those from your for-loop limits. If "randomXSize" is a constant (at least there's no code that changes it during the iterations) all inner arrays will have the same size. However if you plan to change "randomXSize" in each "a"-iteration, each nested array will have a different size. Keep that in mind when you later work with the array.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Determining groups in a 2D array by checking neighbours (and their neighbours etc) 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

play only one door animation 1 Answer

Array of game objects 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