Unity 3d参数超出范围

时间:2015-11-01 02:00:46

标签: c# unity3d

此脚本用于查找最靠近“目标”的网格中的下一个节点。代码工作一次,然后告诉我“参数超出范围异常。参数名称:索引。”我相信错误发生在“tempNode = currentPath [m_index];”上。但是在我的头撞到键盘一小时之后我不再确定了。 currentPath aways中至少有一个项目,无论有多少都会抛出错误。

Unity没有具体告知我错误的位置,只是它是一个List错误。想法?

我知道它来自代码的这一部分,但我已经把它包含在整个代码中。

else 
{
    pathFound = false;
    if (currentPath.Capacity > 0 && pathFound == false)
    {
        int m_index = (currentPath.Count - 1);
        List<PathNodes> tempNodeList;
        PathNodes tempNode;
        tempNode = currentPath[m_index];
        tempNodeList = tempNode.connections;
        if (tempNodeList == null)
        {
            Debug.Log("Paths not built");
        }
        else
        {
            currentPath.Add(GetNextNode(tempNodeList, destinationNode.gameObject).GetComponent<PathNodes>());
            EditorUtility.SetDirty(this);
            tempNode = null;
            tempNodeList = null;
        }
    }
}

整个代码

[SerializeField] public GameObject currentNode;
[SerializeField] public GameObject destinationNode;
[SerializeField] public GameObject destination;
[SerializeField] public GameObject[] allPathNodes;
public List<PathNodes> currentPath;
public List<PathNodes> avoidPath;

void findPath()
{
    bool pathFound;

    if(allPathNodes == null)
    {
        allPathNodes = GameObject.FindGameObjectsWithTag("PathNode");
    }


    if (currentPath.Capacity == 0)
    {
        currentPath.Add(GetClosestNode(allPathNodes, gameObject).GetComponent<PathNodes>());
        EditorUtility.SetDirty(this);
    }

    if (currentNode == null && currentPath[0] != null)
    {
        currentNode = currentPath[0].gameObject;
    }

    if (destinationNode == null)
    {
        if (destination == null)
        {
            Debug.Log("Destination not set");
        }
        else
        {
            destinationNode = GetClosestNode(allPathNodes, destination);
            EditorUtility.SetDirty(this);
        }
    }
    if (currentPath[currentPath.Capacity - 1].gameObject == destinationNode)
    {
        pathFound = true;
    }
    else 
    {
        pathFound = false;
        if (currentPath.Capacity > 0 && pathFound == false)
        {
            int m_index = (currentPath.Count - 1);
            List<PathNodes> tempNodeList;
            PathNodes tempNode;
            tempNode = currentPath[m_index];
            tempNodeList = tempNode.connections;
            if (tempNodeList == null)
            {
                Debug.Log("Paths not built");
            }
            else
            {
                currentPath.Add(GetNextNode(tempNodeList, destinationNode.gameObject).GetComponent<PathNodes>());
                EditorUtility.SetDirty(this);
                tempNode = null;
                tempNodeList = null;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

问题是使用List<T>.Capacity。容量as stated in the documentation始终大于或等于列表中的项目数Count

Capacity是列表在没有需要调整大小的后备存储(数组)的情况下可以容纳的元素数。由于调整大小操作很昂贵(创建一个新数组,复制现有元素,在旧数组被销毁之前),Capacity可用于确定在一个列表中可以交换多少数据。高性能方式(您还可以在构造List时定义初始容量)。

所以问题从这一行开始:

if (currentPath.Capacity == 0)

由于Capacity可能大于当前的Count,因此您可能会遇到错误,因为if (currentPath[currentPath.Capacity - 1].gameObject == destinationNode) 可能大于当前的Capacity

Count

修复方法是将if (currentPath.Capacity > 0 && pathFound == false) 替换为Capacity。这条线也是如此:

Count

为了使Count等于startRecording(),您需要执行List<T>.TrimExcess()调用来调整数组的大小,但要注意这是一个比保持数组更昂贵的操作少量过量并使用stopRecording()代替。