缺少参考例外Unity C#

时间:2017-05-23 00:50:41

标签: c# list unity3d

我在我的Unity游戏中遇到了与我的敌人列表有关的问题,如果一个组为空,它仍将通过for循环运行,导致缺少引用异常,引用了“enemyGroupList [i] [j] ] .enabled = true;'在错误中排队并且不生成新组。我认为它并没有删除空列表,但我似乎无法弄清楚它为什么不是。

在我将for循环作为

之前,我没有遇到过这个问题
for(int j = enemyGroupList.Count - 1; i>=0; i--)

这是一个超出范围异常的争论,并不是我需要的东西,但它会正确更新组并在需要时产生一个新组。

void Update () {
    if(enemyGroupList.Count < groupNumber)
    {
        enemyGroupList.Add(createGroup());
    }

    // for each list in the group list
    for(int i = enemyGroupList.Count - 1; i >= 0; i--)
    {
        // for each opject in a given list
        for (int j = enemyGroupList[i].Count - 1; j >= 0; j--)
        {

            // pause and unpause enemies when the player pauses the game or is talking
            if (player.CurrState == PlayerScript.State.Paused || player.CurrState == PlayerScript.State.Talking)
            {
                enemyGroupList[i][j].enabled = false;
            }
            else
            {
                enemyGroupList[i][j].enabled = true;
            }

            // remove dead enemies
            if (enemyGroupList[i][j].Dead)
            {
                enemyGroupList[i].RemoveAt(j);
                // break avoids out of range exceptions
            }
        }
        if (enemyGroupList[i].Count == 0 || player.CurrState == PlayerScript.State.Win)
        {
            enemyGroupList.RemoveAt(i);
        }
    }
}

由于

0 个答案:

没有答案
相关问题