method to display random question in C#
Hi, I want to display in textfield a random question every time I play the scene and check answer ( unity - C#) Thank you.
private Question[] questions = new Question[3];
public Text question ;
public InputField answer;
void Start(){
questions [0] = new Question ();
questions [0].question = "What is 10+10";
questions [0].correctAnswer = "20";
questions [1] = new Question ();
questions [1].question = "What is 20+20";
questions [1].correctAnswer = "40";
questions [2] = new Question ();
questions [2].question = "What is 30+30";
questions [2].correctAnswer = "60";
//int random = Random.Range (0,3);
textQuestion.text = questions[0].question ; //need a method
}
It works.Thank you for answer EIDo.Any tips how can I use a drop down list with 3 answers ...say and let player choose the correct one, ins$$anonymous$$d of using an inputfield to type in answer.Thanks.
Answer by ElDo · Nov 14, 2015 at 02:48 PM
Don't this work or what's the Problem here?
private Question[] questions = new Question[3];
public Text question ;
public InputField answer;
private int selection;
void Start(){
questions [0] = new Question ();
questions [0].question = "What is 10+10";
questions [0].correctAnswer = "20";
questions [1] = new Question ();
questions [1].question = "What is 20+20";
questions [1].correctAnswer = "40";
questions [2] = new Question ();
questions [2].question = "What is 30+30";
questions [2].correctAnswer = "60";
selection = Random.Range (0,2);
textQuestion.text = questions[selection].question ; //need a method
}
public void CheckAnswer(){
if(answer.text == questions[selection].correctAnswer){
//do something, answer correct
}else{
//do something, answer incorrect
}
}
you'll have to perform CheckAnswer() with your Answer Button.
Note:
selection = Random.Range (0,2);
This will only randomly select the first two questions, out of the three questions. Random.Range for integers is exclusive of the max range. Consider using the length of the array ins$$anonymous$$d of a literal.
selection = Random.Range(0, questions.Length);
Thanks for replay. It works. Any idea how to use a drop down list with 3 choices and let the player pick the correct one ins$$anonymous$$d of using an inputfield to type in correcr answer... $$anonymous$$
use Dropdown answers
ins$$anonymous$$d and add possible items to it with answers.AddRange()
List<string> Options=new List<string>();
Options.Add(questions[selection].correctAnswer);
Options.AddRange(questions[selection].wrongAnswers);
for (int i = 0; i < Options.Count; i++) {
string temp = Options[i];
int randomIndex = Random.Range(i, Options.Count);
Options[i] = Options[randomIndex];
Options[randomIndex] = temp;
}
answers.AddRange(Options);
Thanks EIDo. Nice piece of work,I am sure it works. I think it is something I am missing. reffering to code above: questions [0] = new Question (); questions [0].question = "What is 10+10"; questions [0].correctAnswer = "20"; //I added question[0].wrongAnswer1 = "a"; question[0].wrongAnswer2="b";
same for the other questions, I declared dropDown answers. The line : answers.AddRange(Options); -error- wrong argument. The problem is wrongAnswers, //Options.AddRange(questions[selection].wrongAnswers); how can I add wrongAnswer1 and wrongAnswer2 to wrongAnswers? or I was wrong to declare wrongAnswer1="a" and wrongAnswer2="b"?
One last thing, how does method CheckAnswer changes having a dropdown list? Thank you for taking time to answer my questions. $$anonymous$$
Check Answer might still work. your wrongAnswers must be declared as a List<string>
and you Access them just like an Array by
questions[selection].wrongAnswers[-index of answer 0-based-]
so it would be questions[0].wrongAnswers[0]="a"; questions[0].wrongAnswers[1]="b";
and so on.
Answer by Statement · Nov 14, 2015 at 02:44 PM
Pick a random number between 0 and the number of questions -1.
int random = Random.Range(0, questions.Length);
Then use this random number to select a question.
textQuestion.text = questions[random].question;
Answer by daniel2016 · Nov 16, 2015 at 11:57 AM
HI EIDo.Thanks again for replay. I am sorry, but cant get it right.
private Question[] questions = new Question[4]; private WrongAnswers[] wrongAnswers = new WrongAnswers[4]; private int selection; public Text textQuestion ; public Dropdown answers;
void Start(){
questions [0] = new Question ();
questions [0].question = "What is 10+10";
questions [0].correctAnswer = "20";
qustion[0].wrongAnswers[0] = "a";
question[0].wrongAnswers[1]="b";
question[0].wrongAnswers[2]="c";
selection = Random.Range (0, questions.Length);
List wrongAnswers = new List(); List Options=new List(); Options.Add(questions[selection].correctAnswer); Options.AddRange(questions[selection].wrongAnswers);
loop
answers.AddRange(Options);
Can you tell me what am I missing?? Tahnks again