0

I'm new in Unity I have followed the Unity quiz Game Tutorial And I would like to set it up so that the answers data are showing up randomly. Can anyone help me?

    void ShowQuestion()
    {
        RemoveAnswerButtons();
        ChooseQuestion(); // random question is succsessfull

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

Here my randomize Question

void ChooseQuestion()
        {
            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
                }
          }
        }

        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)
            {
                playerScore += currentRoundData.pointsAddedForCorrectAnswer;                    // If the AnswerButton that was clicked was the correct answer, add points
                scoreDisplay.text = playerScore.ToString();
                scoreDisplay1.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();
            }
        }

Somehow I'm stuck on how to randomize Answer. Can someone help me?

Grisia D
  • 1
  • 2

1 Answers1

1

Using LINQ you can do something like this if you just want to randomize your answers array.

using System.Linq;

void ShowQuestion()
{
    RemoveAnswerButtons();
    ChooseQuestion(); // random question is succsessfull

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

    Random rnd=new Random();
    var answersInRandomOrder = questionData.answers.OrderBy(x => rnd.Next()).ToArray();  

    for (int i = 0; i < answersInRandomOrder.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(answersInRandomOrder[i]);                                    // Pass the AnswerData to the AnswerButton so the AnswerButton knows what text to display and whether it is the correct answer
    }
}       
Ryanas
  • 1,757
  • 3
  • 19
  • 36
  • I tested it. It's still failed. Assets/Scripts/GameController.cs(82,48): error CS1503: Argument `#1' cannot convert `string' expression to type `AnswerData' – Grisia D Mar 13 '18 at 16:26
  • Try now. I've turned the string array into a var so it can just infer the type as I don't know what AnswerData is – Ryanas Mar 13 '18 at 16:46