Terrain Modification Script index out of range?

I am making a script to make my terrain blocky like in minecraft but since I am new to unity3d’s terrain class it is proving a challenge, I do not know why but I get a IndexOutOfRangeError on line 23. Also if you see any other problems please tell me!


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class TerrainToBlocks {

	public static int xRes;
	public static int yRes;

	public static void GetTerrainValues(TerrainData td,GameObject terrainObject){
		Debug.Log ("Started");
		xRes = td.heightmapWidth;
		yRes = td.heightmapHeight;
		float[,] heights = td.GetHeights (0, 0, td.heightmapWidth, td.heightmapHeight);
		RoundNumbers (xRes,yRes,heights,terrainObject);
		Debug.Log ("Step 1 Complete.");
	private static void RoundNumbers(int x, int y,float[,] old,GameObject terrainObject){
		float[,] newHeights = new float[xRes,yRes];
		for(int i = 0; i < x; i++){
			for(int k = 0; i < y; k++){
				newHeights[i,k] = Mathf.RoundToInt(old[i,k]);
		GenerateBlocks (newHeights,x,y,terrainObject);
		Debug.Log ("Step 2 Complete.");

	private static void GenerateBlocks(float[,] hMap,int x, int y,GameObject terrainObject){
		for(int i = 0; i < x; i++){
			for(int k = 0; i < y; k++){
				float xVal = (float)i;
				float yVal = (float)hMap[i,k];
				float zVal = (float)k;
				Vector3 pos = new Vector3(xVal,yVal,zVal);
				GameObject newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
				newCube.transform.position = pos;
		Object.DestroyImmediate (terrainObject);
		Debug.Log ("Terrain Generation Finished.");


in your inner y loop you used

for(int k = 0; i < y; k++){

It should be

for(int k = 0; k < y; k++){

Hav a look for the i=>k :wink: