How to create randomly placed answers?

Hi,

From the 4 buttons, I would like to make the answers randomly placed for each question but I have no idea how to go about it. Is there any chance you could show me a method or point me in the right direction please?

Kind Regards

Paul

Hi Paul,

Welcome to our community. :slight_smile:

That sounds like an interesting idea, which needs a bit of planning. If you are willing to challenge yourself, I’m sure you could accomplish it.

Here is the current concept:

Buttons

Index Name
0 Button 1
1 Button 2
2 Button 3
3 Button 4

Answers

Index Name
0 Answer 1
1 Answer 2
2 Answer 3
3 Answer 4

As you can see, the indices match perfectly. Button[0] refers to Answer[0], Button[1] refers to Answer[1] and so on. If our correct answer is “Answer 3”, clicking Button[2] “selectes” the correct answer, which is Answer[2].

Your idea might result in this:

Answers

Index Name
0 Answer 4
1 Answer 1
2 Answer 2
3 Answer 3

The correct answer is still “Answer 3”, but it is not at index 2 anymore but suddenly at index 3. You would somehow need to override the correct answer index. And this could result in unexpected problems because the correct answer index is just a number, and so are the indices. They are not connected in any logical way that you could determine via code.

For this reason, it is better to leave the original data untouched.

What you could do is to create a new array of integers that maps your random indices to the original data.

Random indices

Index Random index
0 1
1 2
2 0
3 3

The values on the left side remain the same. You could use them for your buttons. If you click button[0], you “look up” the index in the second column and use it for the answer: Answer[1].

Our correct answer is still “Answer 3”. In the original answers array, it is at index 2. Since you did not change it, Button[1] would points to Answer[2] because of the “random indices” array.

Did this make sense?

If so, create this “translation layer” in your code. Populate the “random indices” array with hard-coded values. Do not randomise them via code yet. The first step should be to change the order of the answers without changing the original data and without losing the correct answer index.

Once you accomplished that, you can look for a solution for randomising integers.

Let me know how it worked. :slight_smile:


See also:

I did it. Woo. First I made every QuestionSO’s first answer the correct one. Then I randomised a list and made the buttons go in a random order. To get the correct answer index is simply picking the first element of the random list; The edited Methods:

    public void DisplayQuestion()
    {
        questionText.text = currentQuestion.GetQuestion();
        /////////////////////// RANDOM PLACED ANSWERS
        shuffledList = new List<int>() {0,1,2,3};
        shuffledList = shuffledList.OrderBy( x => Random.value ).ToList();
        foreach(int i in shuffledList)
            Debug.Log(i);
        for(int i = 0; i < answerButtons.Length; i++)
        {
            TextMeshProUGUI buttonText = answerButtons[shuffledList[i]].GetComponentInChildren<TextMeshProUGUI>();
            buttonText.text = currentQuestion.GetAnswer(i);
        }
    }

    void DisplayAnswer(int index)
    {
        Image buttonImage;
        int correctAnswerIndex = shuffledList[0];               // 0 points to new correct answer index
        Debug.Log("correct index: " + correctAnswerIndex);

        if(index == correctAnswerIndex) // corect answer
        {
            questionText.text = "Correct answer.\nWell Done.";
            scoreKeeper.IncrementCorrectAnswers();
        }
        else                            // incorrect answer
        {
            string answer;
            if(hasAnswered) // answered question wrongly
            {
                buttonImage = answerButtons[index].GetComponent<Image>();
                buttonImage.color = Color.red;
                answer = "Incorrect.";
            }
            else            // didn't answer question at all
            {
                answer = "Out of time.";
            }
            answer += " The correct answer is\n" + currentQuestion.GetCorrectAnswer();
            questionText.text = answer;
        }

        buttonImage = answerButtons[correctAnswerIndex].GetComponent<Image>();
        buttonImage.color = Color.green;
    }

I don’t fully understand the random list part I just know it works and uses using System.Linq;

1 Like

Awesome! :slight_smile:

I cannot believe that I didn’t come up with this obvious solution myself! Well done. :slight_smile:

I don’t fully understand the random list part I just know it works and uses using System.Linq;

That’s fine. In programming, we use “black boxes” all the time because we do not want to develop every single solution ourselves.

Keep up the good work. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms