2016-08-15 81 views
0

我正在进行问答游戏。我有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()); 
} 

}

+0

我不确定单词“直到”是你实际上的意思..“直到”继续浇水,直到杯子已满.. – BugFinder

+0

我的意思是,如果所有的10个问题都回答它将移动到下一个面板。 – Magillanica

+0

那么你具体有什么问题? – BugFinder

回答

0

由于没有设置你的StartCoroutine我只能假设你想这是什么

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

我的问题是即使我回答了所有问题,游戏仍然会产生问题。这是解决方案吗? – Magillanica

+0

我已经用这段代码替换了我的IEnumerator,但它仍然在运行 – Magillanica

0

这就像每次重置场景时接下来的问题是你有空的未被应答的问题列表,并不断从问题列表中重新创建它。

private static List<Question> unansweredQuestions; 

... 

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

    SetCurrentQuestion(); 

} 

和新问题,你这样做:

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

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

+0

仍然无法工作,但我试过了:if(unansweredQuestions == null) 现在停止了,当我回答所有的问题,但它说“论据已超出范围。”我想让它移动到下一个菜单,比如分数统计后,我完成了回答所有问题 – Magillanica

+0

,然后尝试如下所示: 'if(unusedweredQuestions.Count> 0) SetCurrentQuestion(); 其他 moveToNextMenu();' – Krajca

+0

我在 'moveToNextMenu()得到了一个错误;' 这里是我的代码 '无效的start() \t { \t \t如果(unnsweredQuestions == null) \t \t { \t \t \t未答复的问题=提问。ToList (); \t \t} \t \t SetCurrentQuestion(); \t \t}' – Magillanica