Putting answer randomly in options

Hi guys

I am generating random number of objects when game starts, and destroying them after some time. Then I am showing GUI with three options(3 numbers) and user has to select the correct one(i.e how many number of objects were there).

I don’t know how to put the answer in one of the three options in random spots each time and then fill other two options with number close to the answer. (Note that objects are also getting generated randomly when game starts)

So basically what I want is if 4 objects are generated, then GUI will pop up with three option and within the three options, one will be the answer i.e 4 and other two options will be 5,6 or 3,5 etc.

Currently I am just hard coding the value as shown below.

#pragma strict


var respawn1:GameObject;
var randomcars:int;
var i:int;
var showGUI = false;
var myCam:Camera;

function Start () {

randomcars = Random.Range(1,6);

for (var i : int = 1;i <= randomcars; i++) {
		Instantiate (respawn1, Vector3(i * 5, 0, 0), Quaternion.identity);
	}

}

function Update (){
showGUI = (GameObject.FindWithTag("Cars") == null);

}

function OnGUI() {

if (showGUI) {

GUI.Box (Rect (myCam.pixelWidth/2-75,myCam.pixelHeight/2-45,150,90), “How many cars?”);

if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2-25,80,20), "1")) {

if(randomcars==1){
        Application.LoadLevel ("Level2");
    }
}

if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2,80,20), "2")) {

if(randomcars==2){
        Application.LoadLevel ("Level2");
    }
}



if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2+25,80,20), "3")) {

if(randomcars==3){
        Application.LoadLevel ("Level2");
    }
}

}
}

Hi,

you need to write ranomize function that depends on what do you want to achive.
Exapmle of one of many ways you can write this function is below. It is c# code, but it should not be problem to rewrite it to UnityScript.

	public List<int> GetRadnomAnswers(int correctAnswer, int numberOfAnswers, int dispersion)
	{
		//input check
		if (numberOfAnswers <= 0)
			return new List<int>(); //we do not want any answer

		List<int> randomAnswers = new List<int>();

		if (dispersion < 1)
		{
			randomAnswers.Add(correctAnswer);
			return randomAnswers; // no dispersion set, return only answer
		}

		if (dispersion == 1)
		{
			//we do not need randomnes
			randomAnswers.Add(correctAnswer - 1);
			randomAnswers.Add(correctAnswer);
			randomAnswers.Add(correctAnswer + 1);

			return randomAnswers;
		}


		//main loop
		int bottomLimit = Math.Max(1, correctAnswer - dispersion); //botom limit is inclusive
		int topLimit = correctAnswer + dispersion + 1; //top limit is exclusive


		while (randomAnswers.Count < numberOfAnswers)
		{
			int randomAnswer = Random.Range(bottomLimit, topLimit);

			if (randomAnswers.Contains(randomAnswer))
				continue; //randomAnswer already in list

			randomAnswers.Add(randomAnswer);
		}

		//check if right answer is between random answers
		if (randomAnswers.Contains(correctAnswer) == false)
		{
			//it is not, we nedd to add it to random place
			int randomIndex = Random.Range(0, randomAnswers.Count + 1);
			randomAnswers[randomIndex] = correctAnswer;
		}

		return randomAnswers;
	}

You can call this function like this:

List<int> randomAnswers = GetRadnomAnswers(randomcars, 3, 2);

		//your gui will work only for exactly 3 answers
		if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2-25,80,20), randomAnswers[0].ToString())) {
			
			if(randomcars == randomAnswers[0]){
				Application.LoadLevel ("Level2");
			}
		}

		if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2,80,20), randomAnswers[1].ToString())) {
			
			if(randomcars == randomAnswers[1]){
				Application.LoadLevel ("Level2");
			}
		}

		if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2+25,80,20), randomAnswers[2].ToString())) {
			
			if(randomcars == randomAnswers[2]){
				Application.LoadLevel ("Level2");
			}
		}