大家好,我正在为我的项目在uni制作一个立方体交换游戏,我有一个问题,我的代码试图访问不存在的数组地址,虽然我不知道它是如何访问它的。出于某种原因,在第二次或第三次传递时,它将l,m和n设置为5,因为我的数组长度只有5x5x5,所以无法找到任何东西。
public void matchCheck()
{
int l;
int m;
int n;
//sweep x axis for matches
for (l = 0; l < 5; l++) // the letter k is unclean and must be purged
{
for (m = 0; m < 5; m++)
{
for (n = 0; n < 5; n++)
{
if (n > 0)
{
if (grid[n - 1, m, l] == grid[n, m, l] && xcombo < 6)
{
xcombo += 1;
}
else
{
switch (xcombo)
{
case 4:
Debug.Log("match 5" + cube[n - 1, m, l] + cube[n - 2, m, l] + cube[n - 3, m, l] + cube[n - 4, m, l] + cube[n - 5, m, l]);
grid[n - 1, m, l] = randomNumber();
grid[n - 2, m, l] = randomNumber();
grid[n - 3, m, l] = randomNumber();
grid[n - 4, m, l] = randomNumber();
grid[n - 5, m, l] = randomNumber();
xcombo = 0;
refreshGrid();
break;
case 3:
Debug.Log("match 4" + cube[n - 1, j, l] + cube[n - 2, j, l] + cube[n - 3, j, l] + cube[n - 4, j, l]);
grid[n - 1, m, l] = randomNumber();
grid[n - 2, m, l] = randomNumber();
grid[n - 3, m, l] = randomNumber();
grid[n - 4, m, l] = randomNumber();
xcombo = 0;
refreshGrid();
break;
case 2:
Debug.Log("match 3" + cube[n - 1, j, l] + cube[n - 2, j, l] + cube[n - 3, j, l]);
grid[n - 1, m, l] = randomNumber();
grid[n - 2, m, l] = randomNumber();
grid[n - 3, m, l] = randomNumber();
xcombo = 0;
refreshGrid();
break;
}
Debug.Log("combo" + xcombo);
xcombo = 0;
refreshGrid();
}
}
}
n = 0;
}
m = 0;
xcombo = 0;
}
}
答案 0 :(得分:1)
grid[n - 5
当n为低时,那个和所有相似的行(n-1除外)将导致负数。
此外,你的for循环从0开始,但你会立即检查它是否大于0.为什么不在1开始呢?