停止生成问题

时间:2016-08-15 12:01:52

标签: c# unity3d

我正在做一个问答游戏。我有10个问题,但在回答了所有问题之后,它只是不断产生问题,所以我想在这里发生的只是回答10个问题并转到下一个面板或场景的得分结果。 请帮帮我。

public Question[] questions;
private static List<Question> unansweredQuestions;

private Question currentQuestion;

[SerializeField]
private Text questionText;

[SerializeField]
private Text trueAnswerText;

[SerializeField]
private Text falseAnswerText;

[SerializeField]
private Animator animator;

[SerializeField]
private float timeBetweenQuestions = 1;

void Start()
{
    if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    {
        unansweredQuestions = questions.ToList<Question>();
    }

    SetCurrentQuestion ();


}

void SetCurrentQuestion() 
{
    int randomQuestionIndex = Random.Range (0, unansweredQuestions.Count);
    currentQuestion = unansweredQuestions [randomQuestionIndex];

    questionText.text = currentQuestion.question;

    if (currentQuestion.isTrue) 
    {
        trueAnswerText.text = "CORRECT";
        falseAnswerText.text = "WRONG";
    }else
    {
        trueAnswerText.text = "WRONG";
        falseAnswerText.text = "CORRECT";

}

}

IEnumerator TransitionToNextQuestion()
{
    unansweredQuestions.Remove(currentQuestion);

    yield return new WaitForSeconds (timeBetweenQuestions);


    SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);

}

public void UserSelectTrue()
{
    animator.SetTrigger ("True");
    if (currentQuestion.isTrue) 
    {
        Debug.Log ("CORRECT");
    } else 
    {
        Debug.Log ("WRONG");
    }

    StartCoroutine(TransitionToNextQuestion());
}

public void UserSelectFalse()
{
    animator.SetTrigger ("False");
    if (!currentQuestion.isTrue) 
    {
        Debug.Log ("CORRECT");
    } else 
    {
        Debug.Log ("WRONG");
    }

    StartCoroutine(TransitionToNextQuestion());
}

}

2 个答案:

答案 0 :(得分:0)

由于未提供StartCoroutine,我只能假设这是您想要的

IEnumerator TransitionToNextQuestion()
    {
        if(unansweredQuestions.Count > 0)
        {
            unansweredQuestions.Remove(currentQuestion);
            yield return new WaitForSeconds (timeBetweenQuestions);
            SceneManager.LoadSceneSceneManager.GetActiveScene().buildIndex);
         } else
            //Move on
    }

答案 1 :(得分:0)

就像每次为下一个问题重置场景一样,你有空的无答案问题列表并继续从问题列表中重新创建它。

private static List<Question> unansweredQuestions; 

...

void Start()
{
    if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    {
        unansweredQuestions = questions.ToList<Question>();
    }

    SetCurrentQuestion ();

}

对于新问题,你这样做:

SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);

编辑: 使用SetCurrentQuestion()更改此LoadScene();应该工作正常