有人可以使用C#统一代码在我的测验游戏中帮助我

时间:2018-04-26 10:26:22

标签: c# unity3d

我是编程的新手,我现在在测验游戏中遇到困难。我的游戏有类别,每个问题都有4个按钮选项供用户选择。问题是当你按下错误的按钮后,你会立即进入类别。有人可以帮我做一个尝试,比如当用户需要按2个错误的按钮然后它会转到类别。

这是我的剧本:

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

public class LevelControlScript2 : MonoBehaviour {

// Get references to game objects that should be disabled and enabled
// at the start
GameObject[] toEnable, toDisable;

// References to game objects that should be enabled
// when correct or incorrect answer is given
public GameObject correctSign, incorrectSign;

// Variable to contain current scene build index
int currentSceneIndex;

// Use this for initialization
void Start () {

    // Getting current scene build index
    currentSceneIndex = SceneManager.GetActiveScene ().buildIndex;

    // Finding game objects with tags "ToEnable" and "ToDisable"
    toEnable = GameObject.FindGameObjectsWithTag ("ToEnable");
    toDisable = GameObject.FindGameObjectsWithTag ("ToDisable");

    // Disabling game objects with tag "ToEnable"
    foreach (GameObject element in toEnable)
    {
        element.gameObject.SetActive (false);
    }

}

// Method is invoked when correct answer is given
public void RightAnswer()
{
    // Disabling game objects that are no longer needed
    foreach (GameObject element in toDisable)
    {
        element.gameObject.SetActive (false);
    }

    // Turn on "correct" sign
    correctSign.gameObject.SetActive (true);

    // Invoke GotoMainMenu method in 1 second
    Invoke ("LoadNextLevel", 1f);

}

// Method is invoked if incorrect answer is given
public void WrongAnswer()
{
    // Disabling game objects that are no longer needed
    foreach (GameObject element in toDisable)
    {
        element.gameObject.SetActive (false);
    }

    // Turn on "incorrect" sign
    incorrectSign.SetActive (true);

    // Invoke GotoMainMenu method in 1 second
    Invoke ("GotoCategories", 1f);
}


// Method loads next level depending on current scenes build index
void LoadNextLevel()
{
    SceneManager.LoadScene (currentSceneIndex + 1);
}

// Method loads Category scene
void GotoCategories()
{
    SceneManager.LoadScene ("Easy");
}

}

1 个答案:

答案 0 :(得分:2)

只需添加一个计数器并检查它:

private int triesLeft;    // Set this to 1 (leave after second) or whatever when a level starts    

// Method is invoked if incorrect answer is given
public void WrongAnswer()
{
    // Disabling game objects that are no longer needed
    foreach (GameObject element in toDisable)
    {
        element.gameObject.SetActive (false);
    }

    // Turn on "incorrect" sign
    incorrectSign.SetActive (true);

    triesLeft--;
    if(triesLeft <= 0)
    {
        // Invoke GotoMainMenu method in 1 second
        Invoke ("GotoCategories", 1f);
    }
}