- Home /
Question by
JoshMBeyer · Dec 12, 2014 at 08:06 AM ·
buttonorderrandomize
How to Randomize the order buttons are displayed instead of forloop?
I am currently using a forloop to display each button in the list. In this case there are 4 buttons to be displayed, so with the forloop its going button0, button1, button2, button3. I would like it to do that but in a random order different each time. like button3, button0, button2, button1, and then next time, button0, button3, button1, button2, or something. Here is how it is at this point.
GUILayout.BeginVertical();
for (int i = 0; i <= 3; i++)
{
if (GUILayout.Button(aList[index].Answers[i]))
{
//If correct answer...
if (i == aList[index].CorrectAnswer)
{
Debug.Log("Correct");
if (index < aList.Count)
{
aList.Remove(aList[index]);
GetQuestion();
} //Here we would add the Ending...
else
{
Debug.LogError("Out Of Questions To Display");
}
}
else //if incorrect....
{
Debug.Log("Incorrect");
GetQuestion();
}
}
}
Comment
Answer by taxvi · Dec 12, 2014 at 08:31 AM
List <string> answers = new List<string> (aList[index].Answers);
for (int i = 3; i >= 0; i--)
{
int myRand = Random.Range(0, i);
if (GUILayout.Button(answers[myRand]))
{
//If correct answer...
if (answers[myRand] == aList[index].Answers[aList[index].CorrectAnswer])
{
Debug.Log("Correct");
if (index < aList.Count)
{
aList.Remove(aList[index]);
GetQuestion();
} //Here we would add the Ending...
else
{
Debug.LogError("Out Of Questions To Display");
}
}
else //if incorrect....
{
Debug.Log("Incorrect");
GetQuestion();
}
}
answers.RemoveAt(myRand);
}
also, add this line at the top:
using System.Collections.Generic;
Your answer
Follow this Question
Related Questions
UI Button order 1 Answer
Advancing through scene elements? 1 Answer
GUI.Label appearing behind GUI.Button 1 Answer
Audio problems with unity after reloading my game from the main menu 1 Answer
touch GUITexture Help 1 Answer