- Home /
Randomize Text on 4.6 UI Button's
So there are 4 button's in the scene. Answer 0 Button - Answer 3 Button, at runtime, their text changes to the text from a class called Question(String Answers, String QuestionText, int CorrectAnswerIndex); Then I have a list of these questions. I am almost done with my project I just need to make it to where the answers are randomized and my work will be complete. Here is the code that handles filling in the button's text. If you need more information I will post more of the script. I am just stuck and can't quite figure this out.
//------------------------------------------------------
void displayQuestion (Question question) {
//------------------------------------------------------
questionText.text = question.QuestionText;
// Variable answers, as well as answerCount,
// introduced for faster access fo some data.
List<string> answers = question.Answers;
int i, answerCount = answers.Count;
if (answerCount > maxNumberOfAnswers)
Debug.Log ("answerCount is greater then maxNumberOfAnswers");
// First, we must enable necessery number of
// buttons and display answer text for them.
for (i = 0; i < answerCount; ++i)
{
answerButton [i].SetActive (true);
answerButtonText [i].text = answers [i];
}
// Then we should hide extra buttons, which is
// not containig answers.
for (i = answerCount; i < maxNumberOfAnswers; ++i)
{
answerButton [i].SetActive (false);
}
} // ~displayQuestion
Answer by HYPERSAVV · Dec 18, 2014 at 01:44 AM
Randomizing your selection should do the trick. With each selection we remove it from the List, and the next iteration we can choose from answerCount - i.
for (i = 0; i < answerCount; ++i)
{
answerButton [i].SetActive (true);
int selectedAnswer = Random.Range(0, answerCount - 1 - i);
answerButtonText [i].text = answers [selectedAnswer];
answers.RemoveAt(selectedAnswer);
}
EDIT: Corrected RemoteAt to RemoveAt
This isn't working actually. I just found a few bugs that are cause by this.
well as i said the issue is with the randomization of the answers. What is happening is that at runtime, a question and the 4 answers are pulled from the list. Question[index] and Answer[0] Answer[1] Answer[2] and Answer[3] Then, they are displayed randomly as text to the buttons. So, the index is involuntarily being switched around. So Answer[1] could be in the answer[3]'s slot. So if the correct answer is Answer[1], i would have to click Answer[3].
To be fair, I have no knowledge of your selection logic so I was going off of what was asked (displaying text randomly) :) Although, RemoteAt should be RemoveAt().
Try to think of this problem a little differently. You're concerned with randomizing the text across multiple buttons, but when we do this we cannot say that Button N maps to Answer[N] anymore. When we set a button with an answer, we need the button to know what index it maps to in the original Answers array.
...or we can just shuffle the Buttons around! Button N will map to Answer[N] now. You will just need the logic to dynamically move the buttons around, perhaps by creating Empty objects in your scene at the location of each possible button, and using those Transforms to shuffle the buttons around.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Random instantiation endlessly? 1 Answer