Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Mint92 · Feb 05, 2014 at 04:24 PM · javascriptrandomspawningmultiple

Spawning random questions at a time

Hey guys, how do you go abouts spawning questions one at a time and randomly. the questions have been set up as an array same with the answers. but its showing all the questions straight away var arrayOfQuestions : Question [];

 class Question
 {
     private var questionText  :  String;
     private var questionRect  :  Rect;
 
     private var answers : String[] = new String[4];
     private var displayAnswers: String[] = new String[4];
     private var answerRects : Rect[] = new Rect[4];
 
     private var correctAnswer : String;
     private var correctRect : Rect;
 
     private var correctAnswerIndex : int; // Zero based
 
     private var questionWidth =300;
     private var answerWidth  = 75;
     private var questionHeight = 1000;
     private var correctWidth = 500;
 
     private var selectedAnswer = -1;
 
     // Constructor
     function Question (pos : Vector2, q : String, a : String, b : String, c : String, d : String, correct : int )
     {
        questionText = q;
        answers[0]   = a;
        answers[1]   = b;
        answers[2]   = c;
        answers[3]   = d;
 
        var st = "ABCD";
 
        for (var i = 0; i < answers.Length; i++) {
          displayAnswers[i] = st[i]+": "+answers[i];   
        }
 
        correctAnswerIndex = correct;
        correctAnswer = "Correct answer: " + answers[correct];
 
        CalcRects(pos);
     }
 
     // Calculate all the rectangles used for display
     function CalcRects(pos : Vector2) 
     {
        var rect : Rect;
        rect.x = pos.x;
        rect.y = pos.y;
        rect.width = questionWidth;
        rect.height = questionHeight;
        questionRect =  rect;
 
        rect.x += questionWidth;
        rect.width = answerWidth;
        for (var i = 0; i < answers.Length; i++) 
        {
          answerRects[i] = rect;
          rect.x += answerWidth;
       }
 
       rect.width = correctWidth;
       correctRect = rect;
     } 
 
     function DisplayQuestion()
     {
        GUI.Label(questionRect, questionText);
        for (var i = 0; i < answers.Length; i++)
        {
          GUI.Label(answerRects[i], displayAnswers[i]);
        }
        if (selectedAnswer != -1)
        {
          GUI.Label(correctRect, correctAnswer);
        }
     }
     
     function CheckForClick(pos : Vector2) {
        for (var i = 0; i < answers.Length; i++) {
          if (answerRects[i].Contains(pos)) {
           selectedAnswer = i;
           break;
          }
        }
     }
 }
 
 function Start ()
 {
     arrayOfQuestions = new Question [5];
   
     arrayOfQuestions[0] = new Question(Vector2(30,0),   "How many league titles have Liverpool Won?", "18", "5", "10", "9",0);
     arrayOfQuestions[1] = new Question(Vector2(30,30),  "Who won the World Cup in 2010?", "Germany", "Italy", "Brazil", "Spain", 3);       
     arrayOfQuestions[2] = new Question(Vector2(30,70),  "Who is the Manager of Manchester United?", "David Moyes", "Rafa Benitez", "Sir Alex Fergerson", "Brendan Rodgers", 0);       
     arrayOfQuestions[3] = new Question(Vector2(30,105), "The Bundesliga is a professional association football league in which country?", "Italy", "Germany", "Norway", "Austria", 1); 
     arrayOfQuestions[4] = new Question(Vector2(30,150), "Which footballer claimed that his hand-ball goal against England in 1986 was ‘The hand of God’?","Diego Maradona ", "Pele", "Wayne Rooney", "Gary Lineker", 0);     
 }
 
 
 function OnGUI ()
 { 
     var e = Event.current;
 
     if (e.type == EventType.Repaint)
     {
         for ( var thisQ : Question in arrayOfQuestions )
         {
         thisQ.DisplayQuestion();       
         }
     }
 
     if (e.type == EventType.MouseDown)
     {
        for ( var thisQ : Question in arrayOfQuestions )
         {
         thisQ.CheckForClick(e.mousePosition);     
         }
     }
 }
Comment
Add comment · Show 12
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 highpockets · Feb 05, 2014 at 05:01 PM 0
Share

You probably want to look into:

Random.Range

In the scripting docs. $$anonymous$$eep in $$anonymous$$d that the higher number is exclusive, so you'll want to have the max range at 1 higher than your array length

avatar image Owen-Reynolds · Feb 05, 2014 at 05:12 PM 0
Share

Seems like more of a general program$$anonymous$$g question. But, if you want no repeats, look at "shuffling" the big Question array.

avatar image Mint92 · Feb 05, 2014 at 06:33 PM 0
Share

im fairly new and noobish at this, firstly as this is spawning all the questions straight away, im assu$$anonymous$$g thats what function start does is doing?

avatar image Mint92 · Feb 05, 2014 at 07:07 PM 0
Share

it just takes me time to understand what to do and to do it. again i do apologise for this.

so i did added the currentquestion array and edited the OnGUI function

 unction OnGUI ()
 { 
     var e = Event.current;// the event thats currently happening 
 
     if (e.type == EventType.Repaint)//other events are processed first before the repaint can actiavte
     {
         for ( var thisQ : Question in currentQuestion )
         {
         thisQ.DisplayQuestion();       
         }
     }
 
     if (e.type == EventType.$$anonymous$$ouseDown)
     {
        for ( var thisQ : Question in currentQuestion )
         {
         thisQ.CheckForClick(e.mousePosition);     
         }
     }
 }

not sure what the error "Argument is not enumerable error" @robertbu

avatar image robertbu · Feb 05, 2014 at 07:36 PM 0
Share

'currentQuestion' is not an array.

 function OnGUI ()
 { 
     var e = Event.current;
  
     if (e.type == EventType.Repaint) 
     {
         currentQuestion.DispalyQuestion();
     }
  
     if (e.type == EventType.$$anonymous$$ouseDown)
     {
       
         currentQuestion.CheckForClick(e.mousePosition);     
     }
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Feb 05, 2014 at 06:44 PM

  • Create a current question variable 'var currentQuestion : Question;'.

  • Rewrite the OnGUI() code so that instead of displaying an array of questions, it display only the current question.

  • To initialize or reset your current question do:


    currentQuestion = arrayOfQuestions[Random.Range(0, arrayOfQuestions.Length)];

If you don't want them to repeat, look into shuffling algorithms as mentioned by @Owen Reynolds. The concept plus sample source has been posted to this list several times.

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

22 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

Related Questions

Spawning different items from different prefabs 2 Answers

Multiple Targets 1 Answer

Help with spawning an object in a random location 1 Answer

Random loot - rarity 3 Answers

How to create random movement in 2D 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