Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by daniel2016 · Nov 14, 2015 at 02:21 PM · arraymethodrandom gen

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          

 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daniel2016 · Nov 14, 2015 at 11:36 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Nov 14, 2015 at 04:01 PM 0
Share

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); 
avatar image daniel2016 · Nov 14, 2015 at 11:13 PM 0
Share

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$$

avatar image ElDo daniel2016 · Nov 15, 2015 at 08:23 AM 0
Share

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);

avatar image daniel2016 · Nov 15, 2015 at 09:46 PM 0
Share

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$$

avatar image ElDo daniel2016 · Nov 16, 2015 at 06:21 AM 0
Share

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.

avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

38 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generic arrays? 1 Answer

Storing functions to use later 0 Answers

Choosing non null values from arrays for pathfinding 2 Answers

Storing a Gameobject from array into a Gameobject variable giving NullReferenceException 3 Answers

Fields not populated during OnValidate on Editor startup 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges