Может ли кто-нибудь помочь мне в моей викторине, используя код на C # Unity - PullRequest
0 голосов
/ 26 апреля 2018

Я новичок в программировании, и сейчас у меня трудные времена в моей викторине. В моей игре есть категории, и у каждого вопроса будет выбор из 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 голосов
/ 26 апреля 2018

Просто добавьте счетчик и проверьте его:

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);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...