- Home /
How can I display 4 items in random order but never double?
So I am displaying 4 GUI Buttons.. Right now I am using a forloop to display them. The only issue is its displaying them the exact same oder everytime. Which i know thats because i havent written otherwise but thats where I need the help. I don't want doubles of an button, just to have them displayed in a random order. Any ideas??
//Display the answers.
for (int i = 0; i <= 3; i++)
{
if (GUI.Button(new Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (25 / 2) + i * 30, buttonWidth, buttonHeight), mathList[mathIndex].Answers[i]))
{
//If correct answer...
if (i == mathList[mathIndex].CorrectAnswer)
{
Debug.Log("Correct");
if (mathList.Count >= 1)
{
Debug.Log("Deleted question" + mathIndex.ToString());
mathList.Remove(mathList[mathIndex]);
GetMathQuestion();
} //Here we would add the Ending...
else { Debug.LogError("Out Of Questions To Display"); }
}
else //if incorrect....
{
Debug.Log("Incorrect");
GetMathQuestion();
}
}
}
Answer by fafase · Dec 14, 2014 at 07:35 PM
Have them in a list and when you draw one remove it from the list. Repeat until the list is empty:
Button[]array;
Button DrawRandomButtonOrder(){
List<Button>list = new List<Button>(array);
while(list.Count > 0) // while the list is not empty
{
int rand = Random.Range(0, list.Count); // Get a random value
if(GUI.Button(list[rand])){} // This line needs more details
list.RemoveAt(rand); // remove it
}
}
I
You need to create it and fill it with the needed info. This is just the algorithm.
Answer by mattyman174 · Dec 14, 2014 at 03:11 AM
Make a list of current chosen buttons and when you go to choose a new random button, check the list to see if the new random button already exists, if it does, then choose another random button instead.
That is not the best approach. What if the list is 1 item long? Endless loop, what if it is 2 items long? 50% chances of endless loop. This is not suitable unfortunately since when there is a chance, it will happen.
Sorry, i thought it was painfully obvious to avoid bad program$$anonymous$$g practices such as that so i didnt include it in my suggestion.
hi @fafase @mattyman174 on what button could i input this code ? do i need another scene? please help me!
Your answer
Follow this Question
Related Questions
Pick between two floats 2 Answers
Selecting random indexes without repetition 3 Answers
Printing a GUI selection grid in order 1 Answer
Problems with removing a random from a list 2 Answers
Printing an ordered list 1 Answer