我尝试使用地图(以及仅包含0&1和1的int数组)来填充布尔值列表,但它会引发以下错误:
NullReferenceException:未将对象引用设置为的实例 对象
在我试图这样做的路上。这是我的代码的一部分,我遇到了以下问题:
public List<bool> free;
void checkSurrounding(int[,] map)
{
int YStart = (int)cell.transform.position.y;
int XStart = (int)cell.transform.position.x;
Debug.Log(YStart + " " + XStart + "THIS");
Debug.Log(map[5, 5]+"THISTHIS");
//bool[] free;
//We check +-1 of the position in the Cellular Automata generated map in both X,Y.
//Went for if statements over for loops for sake of clarity during intial coding, might revist later
if (map[XStart, YStart - 1] == 1) //bool[0]
{
free.Add(true); //It breaks here
}
else if (map[XStart, YStart - 1] == 0)
{
free.Insert(0, false);
}
if (map[XStart - 1, YStart - 1] == 1) //free[1]
{
free.Insert(1, true);
}
else if (map[XStart - 1, YStart - 1] == 0)
{
free.Insert(1, false);
}
if (map[XStart + 1, YStart - 1] == 1) //free[2]
{
free.Insert(2, true);
}
else if (map[XStart + 1, YStart - 1] == 0)
{
free.Insert(2, false);
}
if (map[XStart-1, YStart] == 1) //free[3]
{
free.Insert(3, true);
}
else if (map[XStart - 1, YStart] == 0)
{
free.Insert(3, false);
}
if (map[XStart+1, YStart] == 1) //free[4]
{
free.Insert(4, true);
}
else if (map[XStart + 1, YStart] == 0)
{
free.Insert(4, false);
}
if (map[XStart-1, YStart+1] == 1) //free[5]
{
free.Insert(5, true);
}
else if (map[XStart - 1, YStart + 1] == 0)
{
free.Insert(5, false);
}
if (map[XStart, YStart+1] == 1) //free[6]
{
free.Insert(6, true);
}
else if (map[XStart, YStart + 1] == 0)
{
free.Insert(6, false);
}
if (map[XStart+1, YStart+1] == 1) //free[7]
{
free.Insert(7, true);
}
else if (map[XStart + 1, YStart + 1] == 0)
{
free.Insert(7, false);
}
int test = free.Count;
Debug.Log(test);}
我非常感谢帮助找出发生这种情况的原因。谢谢!