Unity Quiz Game显示不正确的答案

时间:2018-02-20 02:12:42

标签: c# unity3d unity5

我通过Unity中的官方教程制作测验游戏,制作了一个测验游戏多项选择。我试图显示游戏的所有错误/错误答案我该怎么办?这是代码。

显示问题

    void ShowQuestion()
{

    RemoveAnswerButtons();


    bool questionChosen = false;
    while (questionChosen != true) // While question chosen does not equal true
    {
        int random = Random.Range(0, questionPool.Length); // Choose a random number between 0 and the questionPool length

        if (!questionIndexesChosen.Contains(random)) // If the new list doesn't contain the number
        {
            questionIndexesChosen.Add(random); // Add the number to the list
            questionIndex = random; // Set the questionIndex to the number
            questionChosen = true; // Set questionChosen to true to end the while loop
        }
    }

    QuestionData questionData = questionPool[questionIndex];                            // Get the QuestionData for the current question
    questionText.text = questionData.questionText;                                      // Update questionText with the correct text

问题----->按钮

 for (int i = 0; i < questionData.answers.Length; i ++)                             // For every AnswerData in the current QuestionData...
    {
        GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();         // Spawn an AnswerButton from the object pool
        answerButtonGameObjects.Add(answerButtonGameObject);
        answerButtonGameObject.transform.SetParent(answerButtonParent);
        answerButtonGameObject.transform.localScale = Vector3.one;

        AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
        answerButton.SetUp(questionData.answers[i]);                                    // Pass the AnswerData to the AnswerButton so the AnswerButton knows what text to display and whether it is the correct answer
    }
}

void RemoveAnswerButtons()
{
    while (answerButtonGameObjects.Count > 0)                                           // Return all spawned AnswerButtons to the object pool
    {
        answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
        answerButtonGameObjects.RemoveAt(0);
    }
}

和答案按钮单击并显示结果是否正确和错误。

public void AnswerButtonClicked(bool isCorrect)
{
    if (isCorrect)
    {
        Debug.Log("Your Answer is Correct");
        playerScore += currentRoundData.pointsAddedForCorrectAnswer;                    // If the AnswerButton that was clicked was the correct answer, add points
        scoreDisplay.text = playerScore.ToString();

    }

    if (qNumber < questionPool.Length - 1)                                            // If there are more questions, show the next question
    {
        qNumber++;
        ShowQuestion();
    }
    else                                                                                // If there are no more questions, the round ends
    {
        EndRound();
    }
}

1 个答案:

答案 0 :(得分:0)

首先需要结果屏幕,包含信息。你可以创建一个名为ResultsScren的游戏对象,它会让孩子的结果与此类似

ResultsScreen&lt; - 这个具有ResultBehaviour脚本。

 

  • Result1(ResultsScreen&#39; s)
  • Reuslt2(ResultsScreen&#39; s)
  • 结果3(ResultsScreen&#39; s)
  • 你需要一些东西来存储问题,然后才能最终显示结果。

    public List<bool> results = new List<bool>();// you can change this to something better like a Class that contains question ID, result, time used to response....
    public void AnswerButtonClicked(bool isCorrect)
    {
        results.Add(isCorrect); // the position in the array corresponts to the question. For example: Question 1 -> results[0]...Question2 -> results[1]
        if (isCorrect)
        {
            Debug.Log("Your Answer is Correct");
            playerScore += currentRoundData.pointsAddedForCorrectAnswer;                    // If the AnswerButton that was clicked was the correct answer, add points
            scoreDisplay.text = playerScore.ToString();
    
        }
    
        if (qNumber < questionPool.Length - 1)                                            // If there are more questions, show the next question
        {
            qNumber++;
            ShowQuestion();
        }
        else                                                                                // If there are no more questions, the round ends
        {
            EndRound();
        }
    }
    

    当你完成回合/游戏时,你必须绘制你的结果,你必须有这样的方法:

    • 为UI设置预制件并使用预制件(您需要放置在正确的位置)。
    • 你可以拥有一个&#34;硬编码的&#34;屏幕上有位置和带有对象的数组,如果每场比赛/回合的问题总数相同,这可能很有用。

    Script ResultsBehaviour:

    public GameObject[] resultsItem; //Contains the UI GameObjects to print the results
    public GameObject gameObjectWithScriptResults; // Attach the gameobject with the script "AnswerButtonClicked"
    void OnEnable()
    {
       ShowResults();
    }
    void ShowResults(){
       for(int i = 0;i < gameObjectWithScriptResults.getComponent<Script>.results.Count;i++) //this results are from where you have the other script
       {
           //Draw your result[i]
           resultsItem[i].GetComponent<Text>().text = gameObjectWithScriptResults.getComponent<Script>.results[i];
       }
    }
    
    相关问题