terrainData.SetAlphamaps trouble.

I am making a prosedualy generated terrain and am trying to apply textures when a point is at a elevation point. I have two textures in the terrain paint system and am trying to apply it to the terrain if a point is above a height.

		for (int x = 0; x < terrain.terrainData.heightmapWidth; x++) {
			for (int y = 0; y < terrain.terrainData.heightmapHeight; y++) {
				if (pointMap [x, y] > 0.5f) {  //pointMap is a float[,]
					alphaMap [x, y, 0] = 0; 
					alphaMap [x, y, 1] = 1;
				} else {
					alphaMap [x, y, 0] = 1;
					alphaMap [x, y, 1] = 0;
				}
				terrain.terrainData.SetAlphamaps (x, y, alphaMap);
			}
		}

So far it crashes the editor. If it didn’t crash would it work and how do i stop it from crashing.
Or do i have to set the splat-maps by code instead of manually in the editor?

Move line 10 out of the for-loops. You only want to set the alphamaps after the whole alphaMap array has been populated.

for( .... {
.......
}
terrain.terrainData.SetAlphamaps (0, 0, alphaMap);

scripting reference : Unity - Scripting API: TerrainData.SetAlphamaps