I have been working on random generation of objects and I came across an error, like I always do, and I can’t figure this one out. I have been trying to set certain parts of the heightmap, and this was just a test to see if it would work, but the types aren’t converting like they should! it says that in the for loop it can’t convert the Heightmap[x, y] to Heightmap[buildingPlacementX, buildingPlacementY] when it should be able to…
public void GenerateBuilding(Terrain buildingTerrain, GameObject building) {
TD = buildingTerrain.terrainData;
Heightmap = new float[TD.heightmapWidth, TD.heightmapHeight];
float buildingPlacementX = Random.Range(10f, TD.size.x - 10f);
float buildingPlacementZ = Random.Range(10f, TD.size.z - 10f);
int buildingPlacementY = (int)buildingTerrain.SampleHeight(new Vector3(buildingPlacementX, 0, buildingPlacementZ));
Instantiate(building, new Vector3(buildingPlacementX, buildingPlacementY, buildingPlacementZ), Quaternion.identity);
for (float x = 0f; x < (float)TD.heightmapWidth; x++) {
for (float y = 0f; y < (float)TD.heightmapHeight; y++) {
if (Heightmap[x, y] == Heightmap[buildingPlacementX, buildingPlacementZ]) {
Heightmap[x, y] = (float)buildingPlacementY;
}
}
}
TD.SetHeights(0, 0, Heightmap);
}