【问题标题】:Unknown reason for "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index" exception“ArgumentOutOfRangeException:参数超出范围。参数名称:索引”异常的未知原因
【发布时间】:2018-11-01 16:46:19
【问题描述】:

我正在制作一个玩家正在收集有关外星人数据的 Unity 游戏。 因此玩家指向外星人并使用相机之类的东西。

相机 --> 拍摄 Ray --> Ray 返回所有需要的数据,附加到外星游戏对象上的脚本

void ShootRay()
{
    RaycastHit hitInfo; // stores information about hitted object
    if (Physics.Raycast (transform.position, transform.forward, out hitInfo, maxRaycastRange, 1 << LayerMask.NameToLayer("creature"))) // out hitInfo = Unity puts information in the variable hitInfo
    { 
        // UI alerts and collecting dna
        if (hitInfo.distance <= photoRaycastRange) 
        {

            distanceInfo.text = "scanning_genome";


            if (hitInfo.collider.gameObject.GetComponent<EnemyAI> ().dna_collected == false) {

                if (dna_percent_0_to_1 < 1) 
                {
                    calming_dna_scan_circle = false;
                    distanceInfo.text = "scanning_genome";
                    dna_percent_0_to_1 += Time.deltaTime * dna_scanSpeed;
                    dna_collect_circle.fillAmount = dna_percent_0_to_1;
                } 
                else if (dna_percent_0_to_1 >= 1) 
                {

                    // adding info of creature to database
                    if (hitInfo.collider.gameObject.GetComponent<EnemyAI> ().raceIndex == 1) 
                    {
                        if (!raceOneWasAdded) 
                        {
                            BestiariumData.scannedSpecies.Add (hitInfo.collider.gameObject);
                            raceOneWasAdded = true;
                        }
                        BestiariumData.dnaBar_1 += 0.25f;

所提到的数据库只是一个名为 BestiariumData 的类,具有:

public static List<GameObject> scannedSpecies = new List<GameObject> ();

public static List<float> savedDNAFillRates = new List<float> ();

public static float dnaBar_1 = 0;
public static float dnaBar_2 = 0;
public static float dnaBar_3 = 0;
public static float dnaBar_4 = 0;
public static float dnaBar_5 = 0;
public static float dnaBar_6 = 0;
public static float dnaBar_7 = 0;
public static float dnaBar_8 = 0;

}

我有一个菜单,玩家可以在其中检查他/她已经收集了哪些外星人数据。显示外星人的名字(怪物一号,...)以及玩家扫描了多少外星人的进度条。

问题: 如果我尝试分配状态栏的名称,如果抛出 ArgumentOutOfRangeException: Argument is out of range。参数名称:index异常。我通过将另一个脚本中的布尔值设置为 true 来做到这一点。

public List<GameObject> monsterButtons = new List<GameObject>();
public static bool nameButtons = false;


// Update is called once per frame
void LateUpdate () 
{
    if (nameButtons) 
    {
        for (int buttonIndex = monsterButtons.Count; buttonIndex > 0; buttonIndex--) 
        {
            monsterButtons [buttonIndex].GetComponentInChildren<Text> ().text = BestiariumData.scannedSpecies [buttonIndex].name;
        }

    }

}

感谢您的帮助。

【问题讨论】:

    标签: list user-interface unity3d exception button


    【解决方案1】:

    按钮索引提供列表的计数。所以说你的清单包含 10 个项目,计数将是 10。 但是列表的索引从 0 开始,而不是 1。

    因此,当您第一次尝试访问 monsterButtons [buttonIndex] 时,您正在调用索引 10,这意味着项目 11。这不存在因此会引发您的错误。

    要修复,请将“-1”添加到您的索引分配中:

    for (int buttonIndex = monsterButtons.Count -1; buttonIndex >= 0; buttonIndex--) 
        {
            monsterButtons [buttonIndex].GetComponentInChildren<Text> ().text = BestiariumData.scannedSpecies [buttonIndex].name;
        }
    

    【讨论】:

    • 你还需要访问'0th'元素,所以你需要使用buttonIndex >= 0。
    • @ryeMoss 我也打算将其编辑为答案,但随后看到了您的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多