繁体   English   中英

Unity 3d参数超出范围

[英]Unity 3d Argument out of range

该脚本旨在在最接近“目标”的网格中找到下一个节点。 该代码工作一次,然后通知我“参数超出范围异常。参数名称:索引。” 我相信错误在于“ tempNode = currentPath [m_index];”。 但是在将我的头撞在键盘上一个小时之后,我不确定了。 currentPath变量中至少包含1个项目,无论其中有多少个项目都会引发错误。

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;
            }
        }
    }

问题是使用List<T>.Capacity 文档中所述的容量始终大于或等于列表Count的项目Count

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

因此问题开始于这一行:

if (currentPath.Capacity == 0)

出现错误时,由于Capacity可能大于当前Count ,您可能会被扔在这行上:

if (currentPath[currentPath.Capacity - 1].gameObject == destinationNode)

解决方法是用Count替换Capacity 这条线也一样:

if (currentPath.Capacity > 0 && pathFound == false)

为了使Capacity等于Count ,您需要执行List<T>.TrimExcess()来调整数组的大小,但是要知道,这比仅保留少量的过量并使用Count来进行的操作要昂贵得多。 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM