在列表中设置活动的游戏对象

时间:2015-01-31 15:50:23

标签: c# list unity3d gameobject

我正在制作Android 2d应用程序的菜单,我在多个菜单中有一堆UI面板,当我按下或之前的时左边的按钮脚本应该设置下一个面板处于活动状态并取消激活前一个面板,我尝试使用带有游戏对象的公共类进行此操作,但是没有工作并且认为列表应该有效。< / p>

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

  

ArgumentOutOfRangeException:参数超出范围。   参数名称:index

以下是该脚本的相关部分:

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

2 个答案:

答案 0 :(得分:2)

根据您的评论和实际问题,我看到了一个可能的问题。

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");五次(例如),则shipUpgradeSelected的值为5.并且它再次超出范围。所以你需要决定如何限制这个变量的值。

答案 1 :(得分:0)

此代码非常完美:

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帮助我解决问题