Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过单击Unity中的按钮来激活和停用20个面板阵列中的面板_C#_Unity3d - Fatal编程技术网

C# 如何通过单击Unity中的按钮来激活和停用20个面板阵列中的面板

C# 如何通过单击Unity中的按钮来激活和停用20个面板阵列中的面板,c#,unity3d,C#,Unity3d,我正在制作一个儿童教育游戏。 我有20个问题小组。如果回答正确,则会激活一个按钮。如果单击按钮,下一个面板应处于活动状态,最后一个活动面板应处于停用状态。这应该发生在所有20个面板上 代码如下: public class PanelChanger : MonoBehaviour { public GameObject nextButton; public GameObject[] QuestionsPanel; public GameObject wellDonePane

我正在制作一个儿童教育游戏。 我有20个问题小组。如果回答正确,则会激活一个按钮。如果单击按钮,下一个面板应处于活动状态,最后一个活动面板应处于停用状态。这应该发生在所有20个面板上

代码如下:

public class PanelChanger : MonoBehaviour
{
    public GameObject nextButton;
    public GameObject[] QuestionsPanel;
    public GameObject wellDonePanel;

    // Start is called before the first frame update
    public void Start()
    {

        QuestionsPanel[Random.Range(0, QuestionsPanel.Length)].SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {
   
    }

   ...
在这里,我想放置代码以激活下一个面板并停用活动面板。在这段代码中,我可以设置活动的随机面板,但不能停用当前面板


将当前面板存储在字段中

private GameObject currentPanel;
而且总是这样

// If a previous panel exists deactivate it
if(currentPanel) currentPanel.SetActive(false);

// Store the new panel
currentPanel = QuestionsPanel[Random.Range(0, QuestionsPanel.Length)];
// Activate the new panel
currentPanel.SetActive(true);
或者简单地迭代整个数组并执行以下操作

// Get the tendon index
var randomIndex = Random.Range(0, QuestionsPanel.Length);
// Iterate all panels
for(var i = 0; i < QuestionsPanel.Length; i++)
{
    // Set the matching panel active, all others inactive
    QuestionsPanel[i].SetActive(i == randomIndex);
}

非常感谢你的帮助,你正好提出了我要找的东西。非常感谢。
// Get the tendon index
var randomIndex = Random.Range(0, QuestionsPanel.Length);
// Iterate all panels
for(var i = 0; i < QuestionsPanel.Length; i++)
{
    // Set the matching panel active, all others inactive
    QuestionsPanel[i].SetActive(i == randomIndex);
}
using System.Linq;

...

private GameObject [] randomizedQuestions;

private int currentIndex;
private GameObject currentPanel;

private void Start ()
{
    Shuffle ();
    ShowNextQuestion();
}

private void Shuffle()
{
    randomizedQuestions = QuestionPanel.OrderBy(p => Random.value).ToArray();
}

public void ShowNextQuestion ()
{
    if(currentIndex >= randomizedQuestions.Length)
    {
        // Up to you. You can either just stop
        Debug.LogWarning("No more questions available", this);
        return;

        // Or you could just start over 
        Shuffle();
        currentIndex = 0;
    }

    if(currentPanel) currentPanel.SetActive(false);

    currentPanel = randomizedQuestions[currentIndex];

    currentPanel.SetActive(true);

    currentIndex++;      
}