首页 文章

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

提问于
浏览
0

我'm making a menu for an Android 2d app, I have a bunch of UI panels in multiple menu' s当我按下右下角或左上角按钮时,脚本应该设置下一个面板激活并取消激活前一个面板,我认为列表应该有效 .

在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 回答

  • 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 的 Value 't clamp (or don' . 例如,你有

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

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

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

相关问题