我正在尝试编写一个脚本,通过生成高度图然后填充地形块来动态生成世界块。我的问题是如何创建一个二维对象数组。
public class Chunk
{
public Block[,] blocks;
Generate(){
//code that makes a height map as a 2 dimensional array as hightmap[x,y]=z
//convert heightmap to blocks
for (int hmX = 0; hmX < size; hmX++)
{
for (int hmY = 0; hmY < size; hmY++)
{
blocks[hmX, hmY] = new Block(hmX, hmY, heightmap.Heights[hmX, hmY], 1);
}
}
}
}
我遇到了这个错误:
表示对象引用未设置为对象的实例,导致了空引用异常。NullReferenceException was unhandled, Object reference not set to an instance of an object.
Dictionary<Point,Chunk> chunks = new Dictionary<Point,Chunk>();
这样的代码,其中 Point 包含块的坐标。或者,如果它始终是矩形,则可以使用List<List<Chunk>> chunks = new List<List<Chunk>>();
。 - Nathan Andrew Mullenax