Run-time terrains

I’ve spent hours trying to figure out how to add run-time terrain objects. I briefly succeeded in producing a small terrain that appeared in the right spot, but during my attempt to find a way to change the heightmap resolution the terrain no longer showed up, and I haven’t been able to get it to display again after figuring out how to change the resolution.
Here are the crucial parts of my code (from a function that generates the terrain at runtime). I’m hoping someone can tell me what I’m doing wrong:

var TerrainChunk1Data: TerrainData = new TerrainData();
TerrainChunk1Data.heightmapResolution = TerrHtMapRes;
TerrainChunk1Data.size = Vector3(TerrainChunkWidth,TerrainChunkHeight,TerrainChunkLength);
var TCHtMap:float[,] = new float[TerrHtMapRes,TerrHtMapRes];
//fill in the heighmap with a test pattern of all 5s
for (var y:int=0; y<TerrHtMapRes;y++)
{
for (var x:int=0; x<TerrHtMapRes;x++)
{
TCHtMap[y,x] = 5.0;
}
}
TerrainChunk1Data.SetHeights(0, 0, TCHtMap);
var TerrainChunk1Obj = Terrain.CreateTerrainGameObject(TerrainChunk1Data);
TerrainChunk1Obj .transform.position = Vector3(-13,-15,-107);

function Start ()
{
	var data:TerrainData = new TerrainData();
	data.heightmapResolution = 512; 
	data.size = Vector3(2000.0,1.0,2000); 
	var TCHtMap:float[,] = new float[512,512]; 

	for (var y:int=0; y<512;y++)
	{
		for (var x:int=0; x<512;x++)
		{
			TCHtMap[y,x] = Random.Range(0.0, 4.0);
		} 
	}	
	data.SetHeights(0, 0, TCHtMap); 
	var newTerrain:GameObject = Terrain.CreateTerrainGameObject(data) ; 
	newTerrain.transform.position = Vector3(-4,-15,-120);
}

my advice is also to use simple naming in your scripts, don’t make things more complicated then they are.

That works. Many thanks.

The problem with my previous code was that the heightmap resolution was a power of two plus one (33, 129, etc), which is what the documentation and code examples said to use. Appels’ code uses just a power of two (512). Is there a mistake in the documentation? For example, look at the following terrain tutorial and scroll down to the heading “Heightmap Resolution” - http://www.unifycommunity.com/wiki/index.php?title=Terrain_tutorial

The heightmap resolution should be power of two plus one. Also, heights are normalized floats between 0 and 1.

–Eric

Ok, I guess a power of two plus one does work… my height values had been the problem then (5 instead of < 1.0)

yes exactly like Eric said, it should be power off 2 + 1 and the heights between 0 and 1.
i was messing with the script and forgot to correct it before posting.