JekasG
JekasG

Reputation: 145

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

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 ?

EDIT: Yep, sorry about that. I will include what i will want to do here:

So im trying to make jagged array which. So i have a number of platforms, numOfPlatforms I want to go through them, and each platform has its Size which is randomXSize. And now, i want mark a point in each platform which is every 0.5, thats why i made

(randomXSize - 0.5)

But i don't know how many times i will need to do that for so i made

randomXSizeSegments = randomXSize * 2;

To calculate how many 0.5 will be for each randomXSize.

So in other words, If the first platform '0' has a randomXSize of 3

I want the array randomXSizeSegments to be looking like this

randomXSizeSegments[1][0] = 3
randomXSizeSegments[0][1] = 2.5
randomXSizeSegments[0][2] = 2
randomXSizeSegments[0][3] = 1.5
randomXSizeSegments[0][4] = 1
randomXSizeSegments[0][5] = 0.5
randomXSizeSegments[0][6] = 0

And if second platform '1' has a randomXSize of 7 I want the array randomXSizeSegments to be looking like this

randomXSizeSegments[1][0] = 7
randomXSizeSegments[1][1] = 6.5
randomXSizeSegments[1][2] = 6
randomXSizeSegments[1][3] = 5.5
randomXSizeSegments[1][4] = 5
randomXSizeSegments[1][5] = 4.5
randomXSizeSegments[1][6] = 4
randomXSizeSegments[1][7] = 3.5
randomXSizeSegments[1][8] = 3
randomXSizeSegments[1][9] = 2.5
randomXSizeSegments[1][10] = 2
randomXSizeSegments[1][11] = 1.5
randomXSizeSegments[1][12] = 1
randomXSizeSegments[1][13] = 0


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);
        }

    }

}

Upvotes: 0

Views: 566

Answers (1)

Jeff B
Jeff B

Reputation: 8992

In the first iteration of your outer for-loop, randomXPosSegments = new int[a][] creates an "empty" array since a = 0. When you then try to access the "0th" (i.e. first) element of that array, it throws that exception since there isn't a position [0] to access.

To create an array with one element, you need to declare it to have a size of 1:

var singleElementArray = new int[1];
singleElementArray[0] = 42;
Debug.WriteLine(singleElementArray[0]); // Prints '42'

If you give it a size of 0. There isn't a "0th" position that exists to assign to:

var singleElementArray = new int[0];
singleElementArray[0] = 42; // Throws IndexOutOfRangeException

I'm not sure how to help you solve this since you haven't explained what you are trying to accomplish and existing code doesn't make sense (e.g. you have an iterator b you never use, you keep on setting randomXPosSegments[a] to a new empty array).

Edit:

A few things first:

  • To store decimal values like 0.5, randomXPosSegments will need to be an array of double instead of int. We also need to create this outside the loop, or we'll keep creating a new empty array on each iteration. Like wise, you need to initialize randomXPosSegments[a] outside the inner for-loop or you'll just reset it each time.
  • I created a GetRandomXSize() function, since right now it's value never changes.
  • Inside the inner for-loop, I'm using randomXSize - (0.5 * b) to get values like 7, 6.5, .... b will start off as 0, so on the first iteration, it will just be randomXSize. Then it'll subtract a 0.5 for each time it's been through the loop.
  • To get 0 as the last result inside the jagged array, I had to change the condition on the inner for-loop to b <= randomXSizeSegments and make sure I initialize it to be big enough to fit the results (notice the +1 in new double[randomXSizeSegments + 1]).

Here's the code that works for me.

var randomXPosSegments = new double[numOfPlatforms][];
for(int a = 0; a < numOfPlatforms; a = a + 1) {
    var randomXSize = GetRandomXSize();
    var randomXSizeSegments = randomXSize * 2;
    randomXPosSegments[a] = new double[randomXSizeSegments + 1];
    for(int b = 0; b <= randomXSizeSegments; b = b + 1) {
        randomXPosSegments[a][b] = randomXSize - (0.5 * b);
    }
}

Upvotes: 1

Related Questions