- Home /
Question by
HuskyPanda213 · Feb 09, 2014 at 04:53 PM ·
errorterraindatablock
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! Thanks!
Code:
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.");
}
}
Comment
Answer by kingmac2104 · Feb 11, 2014 at 04:34 PM
Hi,
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 ;)