【问题标题】:Set active gameobjects in a list在列表中设置活动的游戏对象
【发布时间】:2015-01-31 15:50:23
【问题描述】:

我正在为 Android 2d 应用程序制作菜单,我在多个菜单中有一堆 UI 面板,当我按下下一个 right 或上一个 left 按钮时脚本应该将下一个面板设置为活动并停用前一个面板,我尝试使用带有游戏对象的公共类来执行此操作,但这不起作用,并且认为列表应该起作用。

在 Unity 编辑器中,我将大小设置为 4 并将 4 个面板拖到脚本中 我收到了这个错误:

ArgumentOutOfRangeException:参数超出范围。 参数名称:索引

这是脚本的相关部分:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MenuControl : MonoBehaviour
{
    //Buttons
    public GameObject ShipUpgradeRight;
    public GameObject ShipUpgradeLeft;

    //Ships
    private int shipUpgradeSelected = 0;
    List<GameObject> ShipList = new List<GameObject>();

    public void Keyword (string keyword)
    {
        if (keyword == "ShipUpgradeLeft") {
            shipUpgradeSelected--;
            ShipList[shipUpgradeSelected].SetActive (true);
            ShipList[shipUpgradeSelected+1].SetActive (false);
        }

        if (keyword == "ShipUpgradeRight") {
            shipUpgradeSelected--;
            CheckShip ();
            ShipList[shipUpgradeSelected].SetActive (true);
            ShipList[shipUpgradeSelected-1].SetActive (false);
        }   
    }
}

【问题讨论】:

  • 首先,我没有看到您在任何地方填充列表...其次,我没有看到您尝试限制值。像这样,您有机会尝试访问索引中不存在的项目。尝试完成调试过程,您会很容易明白我的意思...
  • Walther,填充你的意思是我应该使用 ShipList.Add 吗?但是在统一编辑器中,我将大小设置为 4,我得到了 4 个元素,我将这些元素分配给了游戏对象
  • 如果你有它由编辑器填充,没关系,但你绝对应该尝试通过各种状态,看看你试图在你的方法中访问哪些索引。特别是边界值。这就是导致您的问题的原因:)

标签: c# list unity3d gameobject


【解决方案1】:

根据您的 cmets 和实际问题,我发现了一个可能的问题。

shipUpgradeSelected 的值永远不会增加。此外,shipUpgradeSelected 的初始值为零。

public void Keyword (string keyword)
{
    if (keyword == "ShipUpgradeLeft") {
        shipUpgradeSelected--;
        ShipList[shipUpgradeSelected].SetActive (true);
        ShipList[shipUpgradeSelected+1].SetActive (false);
    }

    if (keyword == "ShipUpgradeRight") {
        shipUpgradeSelected--;
        CheckShip ();
        ShipList[shipUpgradeSelected].SetActive (true);
        ShipList[shipUpgradeSelected-1].SetActive (false);
    }   
}

keyword 等于ShipUpgradeRightShipUpgradeLeft 时,shipUpgradeSelected 的值会减小(因此它小于零)。然后您尝试访问索引小于零的列表项。

但这是第一个问题。

此外,您不钳制(或不循环)shipUpgradeSelected 的值。所以例如你有

if (keyword == "ShipUpgradeRight") {
    shipUpgradeSelected++;
    CheckShip ();
    ShipList[shipUpgradeSelected].SetActive (true);
    ShipList[shipUpgradeSelected-1].SetActive (false);
}

如果调用Keyword("ShipUpgradeRight"); 5 次(例如),shipUpgradeSelected 的值为 5。再次超出范围。所以你需要决定如何钳制这个变量的值。

【讨论】:

  • 你是对的,谢谢你的解释!我现在明白为什么它不起作用了。
【解决方案2】:

这段代码完美运行:

public void Keyword(string keyword) {
         if (keyword == "ShipUpgradeLeft") {
             shipUpgradeSelected--;
             if (shipUpgradeSelected < 0) {
                 shipUpgradeSelected = 0;
                 return;
             }

             ShipList[shipUpgradeSelected].SetActive(true);
             if (shipUpgradeSelected + 1 < ShipList.Count) ShipList[shipUpgradeSelected + 1].SetActive(false);
             else ShipList[0].SetActive(false);
         }
         if (keyword == "ShipUpgradeRight") {
             shipUpgradeSelected++;
             if (shipUpgradeSelected >= ShipList.Count) {
                 shipUpgradeSelected = ShipList.Count - 1;
                 return;
             }

             ShipList[shipUpgradeSelected].SetActive(true);
             if (shipUpgradeSelected > 0) ShipList[shipUpgradeSelected - 1].SetActive(false);
             else ShipList[ShipList.Count-1].SetActive(false);
         }
     }

但如果我要重新开始,我会这样做:

private void Start()
 {
     ShipUpgradeLeft.GetComponent<Button>().onClick.AddListener(() => { Previous(); });
     ShipUpgradeRight.GetComponent<Button>().onClick.AddListener(() => { Next(); });
 }
 public void Next()
 {
     ShipList[shipUpgradeSelected].SetActive(false);
     shipUpgradeSelected = Mathf.Clamp(shipUpgradeSelected++, 0, ShipList.Count - 1);
     ShipList[shipUpgradeSelected].SetActive(true);
 }
 public void Previous()
 {
     ShipList[shipUpgradeSelected].SetActive(false);
     shipUpgradeSelected = Mathf.Clamp(shipUpgradeSelected--, 0, ShipList.Count - 1);
     ShipList[shipUpgradeSelected].SetActive(true);
 }

感谢 walther 和 d12frosted 帮助我解决问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多