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 <em>= st_+": "+answers*;*_</em> 

}

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 = 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_, displayAnswers*);
}
if (selectedAnswer != -1)
{
GUI.Label(correctRect, correctAnswer);
}
}*_

function CheckForClick(pos : Vector2) {
for (var i = 0; i < answers.Length; i++) {
if (answerRects*.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);
}
}
}

  • 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.