c# - Unity 2D Jagged Array , Array index is out of range -



c# - Unity 2D Jagged Array , Array index is out of range -

i have 2d array giving me array index out of range, problem. cause of problem , how can solve ?

edit: yep, sorry that. include want here:

so im trying create jagged array which. have number of platforms, numofplatforms want go through them, , each platform has size randomxsize. , now, want mark point in each platform every 0.5, thats why made

(randomxsize - 0.5)

but don't know how many times need made

randomxsizesegments = randomxsize * 2;

to calculate how many 0.5 each randomxsize.

so in other words, if first platform '0' has randomxsize of 3

i want array randomxsizesegments looking 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 sec platform '1' has randomxsize of 7 want array randomxsizesegments looking 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 = 0; < numofplatforms; = + 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); } } }

in first iteration of outer for-loop, randomxpossegments = new int[a][] creates "empty" array since a = 0. when seek access "0th" (i.e. first) element of array, throws exception since there isn't position [0] access.

to create array 1 element, need declare have size of 1:

var singleelementarray = new int[1]; singleelementarray[0] = 42; debug.writeline(singleelementarray[0]); // prints '42'

if give size of 0. there isn't "0th" position exists assign to:

var singleelementarray = new int[0]; singleelementarray[0] = 42; // throws indexoutofrangeexception

i'm not sure how help solve since haven't explained trying accomplish , existing code doesn't create sense (e.g. have iterator b never use, maintain on setting randomxpossegments[a] new empty array).

edit:

a few things first:

to store decimal values 0.5, randomxpossegments need array of double instead of int. need create outside loop, or we'll maintain creating new empty array on each iteration. wise, need initialize randomxpossegments[a] outside inner for-loop or you'll reset each time. i created getrandomxsize() function, since right it's value never changes. inside inner for-loop, i'm using randomxsize - (0.5 * b) values 7, 6.5, .... b start off 0, on first iteration, randomxsize. it'll subtract 0.5 each time it's been through loop. to 0 lastly result within jagged array, had alter status on inner for-loop b <= randomxsizesegments , create sure initialize big plenty fit results (notice +1 in new double[randomxsizesegments + 1]).

here's code works me.

var randomxpossegments = new double[numofplatforms][]; for(int = 0; < numofplatforms; = + 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); } }

c# for-loop unity3d multidimensional-array jagged-arrays

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -