This question was
closed Feb 21, 2017 at 11:03 AM by
Faysal-010 for the following reason:
The question is answered, right answer was accepted
Question by
Faysal-010 · Feb 16, 2017 at 12:07 PM ·
c#listrandomrandom.rangequiz
How do I get random answers?
Hello,
I am a newbie :P I like to have random answers. So to make it harder for the player so if the question comes he can't remember if it was the first one or the third.
private void ShowQuestion()
{
RemoveAnswerButtons();
// Get random question from the list.
QuestionData questionData = questionPool[Random.Range(0, questionPool.Count)];
// Remove it from the list, so we can't get it again.
questionPool.Remove(questionData);
questionDisplayText.text = questionData.questionText;
for (int i = 0; i < questionData.answers.Length; i++)
{
GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
answerButtonGameObjects.Add(answerButtonGameObject);
answerButtonGameObject.transform.SetParent(answerButtonParent);
AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
answerButton.Setup(questionData.answers[i]);
timeRemaining = currentRoundData.timeLimitInSeconds;
}
}
Comment
Best Answer
Answer by UnityCoach · Feb 16, 2017 at 01:36 PM
If you use UI buttons in a Vertical Layout Group, you can simply change the sibling order or set it as first sibling with some random value.
like :
private void RandomiseButtons (List<Button> buttons)
{
foreach (Button button in buttons)
button.transform.SetSiblingIndex (Random.Range (0, buttons.Count));
}
Answer by alankemp · Feb 16, 2017 at 04:49 PM
You could shuffle your list of answers before you create the buttons.