簡體   English   中英

Unity:禁用/啟用GameObjects並增加Array的索引

[英]Unity: Disabling/Enabling GameObjects and increasing index of an Array

我有一個游戲對象數組,想要激活索引為0的游戲對象,點按一個按鈕后,索引為0的游戲對象會被隱藏,索引為1的游戲對象會被激活,依此類推。

public GameObject[] QuestionList;
private int QuestionsArrayIndex;

public void Start()
    {
        QuestionsArrayIndex = 0;
        QuestionList[QuestionsArrayIndex].SetActive(true);
        Shuffle();
    }

public void ScreenTap()
{
    if (QuestionList[QuestionsArrayIndex].activeInHierarchy)
    {
        QuestionsArrayIndex++;
        QuestionList[QuestionsArrayIndex].SetActive(false);
    }
}
    public void ScreenTap()
    {
        if (QuestionList[QuestionsArrayIndex].activeInHierarchy && QuestionsArrayIndex < QuestionList.Length)
        {
            QuestionList[QuestionsArrayIndex].SetActive(false);
            QuestionsArrayIndex++;
            QuestionList[QuestionsArrayIndex].SetActive(true);
        }
    }

這可能是你想要的方式。 您還需要檢查索引,否則可能會遇到IndexOutOfBounds異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM