为什么仅当返回布尔值时列表计数等于0时List.Count才会给出错误ArgumentOutOfRangeException?

我正在使用Unity。在Update()中,我调用一个返回布尔值的函数。函数的最末端看起来像这样。

        int returnCount = objectsDetected.Count;

        if(objectsDetected.Count == 0){
            return false;
        }else{
            return true;
        }

更新看起来像这样

    void FixedUpdate(){
        if(DetectObjectsRay()){
            GizmoColor = Color.red;
            isHittingDetectable = true;
        }
        else{
            GizmoColor = Color.green;
            isHittingDetectable = false;
        }
    }

整个功能:

private bool DetectObjectsRay(){
        RaycastHit hit;
        List<Vector3> rayDirections = new List<Vector3>();

        foreach(GameObject detectableObject in detectableObjects){
            rayDirections.Add( detectableObject.transform.position - center.position);
        }

        // TODO: This could be written less confusingly
        for(int i = 0; i < rayDirections.Count; i++){
            Debug.DrawRay(center.position, rayDirections[i], Color.yellow);
            if (Physics.Raycast(center.position, rayDirections[i], out hit, MaxConeView)){

                if ((Vector3.Angle(rayDirections[i], center.forward)) <= FOVDegrees * 0.5f){    

                    if(!objectsDetected.Contains(hit.transform.gameObject)){

                        if(detectableObjects.Contains(hit.transform.gameObject)){

                            objectsDetected.Add(hit.transform.gameObject);
                        //  lastDetectedObject = hit.transform.gameObject;
                        }
                    }
                    if(!detectableObjects.Contains(hit.transform.gameObject)){ // If somethin is detected, but it's not an object we want to detect

                        RemoveUndetectedObject(hit.transform.gameObject);
                    }
                }else{ // it's  in reach of cone legth but it is in the fov

                    RemoveUndetectedObject(hit.transform.gameObject);
                }

            }else{ // If something is not detected


                if(objectsDetected.Count > 0){
                RemoveUndetectedObject(objectsDetected[i]);
                }
            }
        }
        Debug.Log("objectdetectedcount: " + objectsDetected.Count);
        int returnCount = objectsDetected.Count;
        //return returnCount > 0 ? true : false;

        if(objectsDetected.Count == 0){
            return false;
        }else{
            return true;
            }
    }

像这样在MonoBehaviour中将objectsDetected声明为变量

public List<GameObject> objectsDetected = new List<GameObject>();

When the count is 1 or more, I do not get any errors. When it is 0, I get an ArgumentOutOfRangeException. When I replace the return statements with Debug.Log("return true/false"), and just put a return true at the end of the function, I get no errors regardless of the objectsDetected count. I am extremely confused. Please let me know if anything is unclear. I have been stuck on this for ages.