- Home /
Array index is out of range?
Hi there
I am beginning a terrain editor and have just started trying to make a diamond square algorithm script. I am currently having an issue with my code where it says array index is out of range. I have spent days trying to solve this issue and would appreciate help.
#pragma strict
var width : int;
var height : int;
var desiredHeight : float;
var step: int;
function Start () {
var terrain = GetComponent(Terrain);
width = terrain.terrainData.heightmapWidth -1;
height = terrain.terrainData.heightmapHeight -1;
step = width;
var heights = terrain.terrainData.GetHeights(0,0,width,height);
for( var j=0; j<width; j = j + step)
for( var i=0; i<width; i = i + step)
print(j);
print(i);
heights[j,i] = .1;
terrain.terrainData.SetHeights(0,0,heights);
}
EDIT
Managed to sort the Array index is out of range problem. However i'm only getting a elevated part at 64,64. Any ideas New code
#pragma strict
var width : int;
var height : int;
var desiredHeight : float;
var step: int;
function Start () {
var terrain = GetComponent(Terrain);
width = terrain.terrainData.heightmapWidth;
height = terrain.terrainData.heightmapHeight;
var heights = terrain.terrainData.GetHeights(0,0,width,height);
width = width-1;
height = height-1;
step = width;
for( var j=0; j<width; j = j + step)
for( var i=0; i<width; i = i + step)
print(j);
print(i);
heights[i,j] = .1;
terrain.terrainData.SetHeights(0,0,heights);
}
Comment
If step = width, and loop is increasing by j+step, wouldn't that be too big?
Well this first test is just trying to do the 4 corners. i and j start at 0, and width = 64. So the way i see it this should raise the elevation at each corner by going [I,j](0,0) then (0,64) then (64,0) then (64,64).