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

Hi , i have this 2D Array which is giving me a Array index is out of range, problem.
What is the cause of this problem and how can i solve this ?

	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] **= 0;**

** randomXPosSegments[a] = new int {(int)(randomXSize - 0.5)};**
** //Debug.Log(b);**
** }**

** }**

** }**

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 nothing...
  // the second time through it is 1, and the third 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. This will cause issues.
    randomXPosSegments[a] = new int[] {(int)(randomXSize - 0.5)};
  }
}

I can’t read anything from that. but let me guess, right now it doesn’t throw an error, right?^^

alright, let me rewrite it the way I think 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 which line the error is thrown.

Not sure why this got bumped after so long, but since there’s still no accepted answer, here’s how you should do this;

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] **=  {(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.