简体   繁体   English

有人可以使用C#统一代码为我的测验游戏提供帮助吗

[英]Can someone help me in my Quiz Game using C# unity code

I'm new to programming and im having a hard time in my quiz game as of now. 我是编程新手,到目前为止我在测验游戏中遇到困难。 There are categories for my game and each question will have a 4 button choices that the user will choose from. 我的游戏分为几类,每个问题都有一个4个按钮供用户选择。 The problem is when you press the wrong button once you will go instantly to categories. 问题是,一旦按下错误的按钮,您将立即进入类别。 Can someone help me make an attempt like when the user needs to press 2 wrong button then it will go to categories. 有人可以帮助我尝试一下,例如当用户需要按2个错误的按钮时,它将进入类别。

Here's my script: 这是我的脚本:

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

} }

Simply add a counter and check against it: 只需添加一个计数器并对其进行检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM