I want to generate a random number several times, using the following function:
for (var i: int = 0; i <4; i + +)
{
randomPick = Random.Range (1,4);
}
But to each number only occur once. How to do it? Thanks in advance.
I want to generate a random number several times, using the following function:
for (var i: int = 0; i <4; i + +)
{
randomPick = Random.Range (1,4);
}
But to each number only occur once. How to do it? Thanks in advance.
Just like that:
import System.Generic.Collections;
var randomNumbers = new List.<int>();
function Start()
{
for (var i = 0; i < 4; i++)
randomNumbers.Add(i);
}
function PickNumber() : int
{
if (randomNumbers.Count <= 0)
return -1; // No numbers left;
var index = Random.Range(0, randomNumbers.Count);
var value = randomNumbers[index];
randomNumbers.RemoveAt(index);
return value;
}
how to stop the repeating questions?? using this script??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class txtcontrol : MonoBehaviour {
int range;
int oldRange;
int score;
public Text Question;
public Text Ans1;
public Text Ans2;
public Text Ans3;
public Text Ans4;
public Text FinalScore;
public Text ScoreText;
public Canvas Gamecan;
public Canvas Losecan;
public Canvas Wincan;
public Button Button_1;
public Button Button_2;
public Button Button_3;
public Button Button_4;
bool btnA;
bool btnB;
bool btnC;
bool btnD;
void Start () {
PlayerPrefs.SetInt ("Level2", 1);
SelectingRange ();
}
void Update () {
}
void SelectingRange()
{
Button_1.interactable = true;
Button_2.interactable = true;
Button_3.interactable = true;
Button_4.interactable = true;
Button_1.GetComponent<Image> ().color = Color.white;
Button_2.GetComponent<Image> ().color = Color.white;
Button_3.GetComponent<Image> ().color = Color.white;
Button_4.GetComponent<Image> ().color = Color.white;
oldRange = range;
range = Random.Range (1, 6);
if (oldRange == range)
{
SelectingRange();
}
else
{
SpawnQuestion ();
}
}
void SpawnQuestion()
{
if(range == 1)
{
Question.text = "1?";
Ans1.text = "oo";
Ans2.text = "yes";
Ans3.text = "maybe";
Ans4.text = "yup";
btnA = true;
}
if(range == 2)
{
Question.text = "2?";
Ans1.text = "oo";
Ans2.text = "yes";
Ans3.text = "maybe";
Ans4.text = "yup";
btnB = true;
}
if(range == 3)
{
Question.text = "3!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 4)
{
Question.text = "4!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 5)
{
Question.text = "5!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 6)
{
Question.text = "6!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 7)
{
Question.text = "7!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 8)
{
Question.text = "8!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 9)
{
Question.text = "10!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 10)
{
Question.text = "10!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 11)
{
Question.text = "11!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 12)
{
Question.text = "12!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 13)
{
Question.text = "13!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 14)
{
Question.text = "14!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 15)
{
Question.text = "15!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 16)
{
Question.text = "16!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
}
public void ButtonA()
{
DisacttiveButtons ();
if (btnA == true)
{
Button_1.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnA = false;
AddScore ();
}
else
{
Button_1.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
public void ButtonB()
{
DisacttiveButtons ();
if (btnB == true)
{
Button_2.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnB = false;
AddScore ();
}
else
{
Button_2.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
public void ButtonC()
{
DisacttiveButtons ();
if (btnC == true)
{
Button_3.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnC = false;
AddScore ();
}
else
{
Button_3.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
public void ButtonD()
{
DisacttiveButtons ();
if (btnD == true)
{
Button_4.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnD = false;
AddScore ();
}
else
{
Button_4.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
void AddScore()
{
score = score + 1;
ScoreText.text = "Score: " + score;
if (score == 15)
{
Invoke ("YouWon", 1);
}
}
void YouLost()
{
Gamecan.gameObject.SetActive (false);
Losecan.gameObject.SetActive (true);
FinalScore.text = "You have correctly answered:" + score + " question!";
}
void YouWon()
{
Gamecan.gameObject.SetActive (false);
Wincan.gameObject.SetActive (true);
}
void DisacttiveButtons()
{
Button_1.interactable = false;
Button_2.interactable = false;
Button_3.interactable = false;
Button_4.interactable = false;
}
}