我正在尝试初始化一个类内的数组。我收到“对象引用未设置为对象的实例”错误。
这是我的NPC类:
namespace EngineTest
{
public class npcs
{
public int tileX, tileY, layerZ;
public int textureX, textureY;
public string ID;
public string state;
public int direction; //0 = south, 1= west, 2 = north, 3= east
public int moveLimitTimer;
public int animationCurrentFrame;
public int animationResetTimer;
public pathPotentials[, ,] pathPotential; (this is the array)
}
}
这是 pathPotentials 类
namespace EngineTest
{
public class npcs
{
public int tileX, tileY, layerZ;
public int textureX, textureY;
public string ID;
public string state;
public int direction; //0 = south, 1= west, 2 = north, 3= east
public int moveLimitTimer;
public int animationCurrentFrame;
public int animationResetTimer;
public pathPotentials[, ,] pathPotential = new pathPotentials[Program.newMapWidth, Program.newMapHeight, Program.newMapLayers];
}
}
我试图用这段代码初始化它:
for (z = 0; z < Program.newMapLayers; z++)
{
for (x = 0; x < Program.newMapWidth; x++)
{
for (y = 0; y < Program.newMapHeight; y++)
{
if(Program.tileNpcs[x, y, z].npcs.Count > 0)
{
Program.tileNpcs[x, y, z].npcs[0].pathPotential[Program.newMapWidth, Program.newMapHeight, Program.newMapLayers] = new pathPotentials();
}
}
}
}
但它不起作用。我该怎么办?提前致谢。