Possible initialized jagged array error C# -
i using jagged array of gameobjects tiled map generation, , keep getting error:
indexoutofrangeexception: array index out of range. (wrapper stelemref) object:stelemref (object,intptr,object) map.generatemap () (at assets/scripts/map.cs:148) map.start () (at assets/scripts/map.cs:74)
i stumped why getting error. below full function have written initialization of jagged array. far know initializing array correctly.
void generatemap(){ mapwidth = random.range (5, 25); mapheight = random.range (5, 25); //initialise jagged array genmaparray = new gameobject [mapheight][]; (int = 0; < mapheight; i++) genmaparray [i] = new gameobject [mapwidth]; //generate mountains int mountx = random.range(1, mapwidth - 1); // random within int mounty = random.range(1, mapheight - 1); // map border (int y = 0; y < mapheight+1; y++) { (int x = 0; x < mapwidth+1; x++) { if((y == 0 && x <= mapwidth) || (y <= mapheight & x == 0) || (y== mapheight && x <= mapwidth) || (y <= mapheight && x == mapwidth)){ genmaparray[y][x] = tiles[0]; }else{ if(y == mounty && x == mountx){ genmaparray[y][x] = tiles[3]; }else{ genmaparray[y][x] = tiles[1]; } } } } //draw tiles on screen (int y = 0; y < mapheight+1; y++) { (int x = 0; x < mapwidth+1; x++) { if (usesquaretiles == true && usehexagontiles == false) { gameobject.instantiate (genmaparray[y][x], new vector3 (x, mapheight - y, 0), quaternion.euler (-90, 0, 0)); } if (usehexagontiles == true && usesquaretiles == false) { float hexwidth = 0.75f / mathf.cos (mathf.deg2rad * 30.0f); gameobject.instantiate (genmaparray[y][x], new vector3 (x * hexwidth, y + (0.5f * mathf.abs (x) % 1), 0), quaternion.euler (-90, 0, 0)); } } } }
Comments
Post a Comment