Question by
Zygia · Oct 03, 2017 at 08:51 AM ·
unity 5gamequalitysettingsquiz
How do I modify my code to not repeat the questions?
I did this quiz but repeats my questions. How can I change the code because he does not repeat it again? I tried with a check on the old value but repeated the same. Thanks for your help!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class QuizScript : MonoBehaviour {
int oldRange;
int range;
int score;
float time = 766;
public Text FinalScore;
public Text ScoreText;
public Texture sfondo;
public Canvas GameCan;
public Canvas LoseCan;
public Canvas WinCan;
public Image TimeBar;
public Button Menu;
public Button Button_1;
public Button Button_2;
public Button Button_3;
bool risp1 = false;
bool risp2 = false;
bool risp3 = false;
bool timego = true;
bool timeLose = true;
public Text Question;
public Text Answ1;
public Text Answ2;
public Text Answ3;
// Use this for initialization
void Start () {
SelectingRange ();
}
// Update is called once per frame
void Update () {
if (time >= 0 && timego) {
time = time - 1;
TimeBar.rectTransform.sizeDelta = new Vector2 (time - 1, 16);
}
else if (time < 0 && timeLose) {
timeLose = false;
Invoke ("HaiPerso", 2);
}
}
void SelectingRange(){
time = 766;
timego = true;
Button_1.interactable = true;
Button_2.interactable = true;
Button_3.interactable = true;
Menu.interactable = true;
Button_1.GetComponent <Image> ().color = Color.blue;
Button_2.GetComponent <Image> ().color = Color.blue;
Button_3.GetComponent <Image> ().color = Color.blue;
Menu.GetComponent<Image> ().color = Color.red;
oldRange = range;
range = Random.Range (1, 10);
if (oldRange == range) {
SelectingRange ();
} else if(oldRange!= range){
SpawnQuestion ();
}
}
void SpawnQuestion(){
if (range == 1) {
Question.text = "Cos'è una RAM?";
Answ1.text = "Un tipo di software";
Answ2.text = "Un sistema operativo";
Answ3.text = "Una memoria ad accesso casuale";
risp3 = true;
}
if (range == 2) {
Question.text = "OS X è un sistema operativo?";
Answ1.text = "Windows";
Answ2.text = "Mac";
Answ3.text = "Linux";
risp2 = true;
}
if (range == 3) {
Question.text = "Blender è?";
Answ1.text = "Una marca di computer";
Answ2.text = "Un software di modellazione 3D";
Answ3.text = "Un sistema operativo";
risp2 = true;
}
if (range == 4) {
Question.text = "Cos'è un 'trojan horse'?";
Answ1.text = "Uno spyware";
Answ2.text = "Un programma che nasconde il suo vero scopo";
Answ3.text = "Un worm";
risp2 = true;
}
if (range == 5) {
Question.text = "Un Gigabyte corrisponde a?";
Answ1.text = "100 Megabyte";
Answ2.text = "1024 Megabyte";
Answ3.text = "10 Terabyte";
risp2 = true;
}
if (range == 6) {
Question.text = "Qual'è l'estensione di un immagine?";
Answ1.text = ".txt";
Answ2.text = ".xls";
Answ3.text = ".jpg";
risp3 = true;
}
if (range == 7) {
Question.text = "Un indirizzo email esatto è?";
Answ1.text = "www.xxx@gmail.com";
Answ2.text = "mariorossigamil.com";
Answ3.text = "Nessuno dei precedenti";
risp1 = true;
}
if (range == 8) {
Question.text = "Un programma consiste di?";
Answ1.text = "Insieme di dati";
Answ2.text = "Raccolta di icone";
Answ3.text = "Sequenza di istruzioni";
risp3 = true;
}
if (range == 9) {
Question.text = "Con la sigla wwww si indica?";
Answ1.text = "Worm Wind Web";
Answ2.text = "World Wide Web";
Answ3.text = "Work Wilds Web";
risp2 = true;
}
if (range == 10) {
Question.text = "Cos'è un hardware?";
Answ1.text = "L'unità disco rigido";
Answ2.text = "Un programma complesso";
Answ3.text = "La parte fisica di un computer";
risp3 = true;
}
}
public void VaiMenu(){
GameCan.gameObject.SetActive (false);
SceneManager.LoadScene (0);
}
public void ProssimoLivello(){
GameCan.gameObject.SetActive (false);
SceneManager.LoadScene (2);
}
public void Button1(){
timego = false;
DisattivaButton ();
if (risp1 == true) {
Button_1.GetComponent <Image> ().color = Color.green;
Invoke ("SelectingRange", 2);
risp1 = false;
AddScore ();
}
else {
Button_1.GetComponent <Image> ().color = Color.red;
Invoke ("SelectingRange", 2);
}
}
public void Button2(){
timego = false;
DisattivaButton ();
if (risp2 == true) {
Button_2.GetComponent <Image> ().color = Color.green;
Invoke ("SelectingRange", 2);
risp2 = false;
AddScore ();
}
else {
Button_2.GetComponent <Image> ().color = Color.red;
Invoke ("SelectingRange", 2);
}
}
public void Button3(){
timego = false;
DisattivaButton ();
if (risp3 == true) {
Button_3.GetComponent <Image> ().color = Color.green;
Invoke ("SelectingRange", 2);
risp3 = false;
AddScore ();
}
else {
Button_3.GetComponent <Image> ().color = Color.red;
Invoke ("SelectingRange", 2);
}
}
public void Ricomincia(){
SceneManager.LoadScene (SceneManager.GetActiveScene().name);
}
void AddScore(){
score = score + 10;
ScoreText.text = "Score: " + score;
if (score==100) {
Invoke ("HaiVinto", 1);
}
}
void HaiPerso(){
GameCan.gameObject.SetActive (false);
LoseCan.gameObject.SetActive (true);
FinalScore.text = "HAI OTTENUTO SOLO: " + score+ " PUNTI";
}
void HaiVinto(){
GameCan.gameObject.SetActive (false);
WinCan.gameObject.SetActive (true);
FinalScore.text = "HAI OTTENUTO: " + score+ " PUNTI";
}
void DisattivaButton(){
Button_1.interactable = false;
Button_2.interactable = false;
Button_3.interactable = false;
}
}
Comment
private List<int> rangeList = new List<int>();
private int result;
private int RandomNumber
{
get
{
if (rangeList.Count <= 0) return -1;
var index = Random.Range(0, rangeList.Count);
result = rangeList[index];
rangeList.RemoveAt(index);
return result;
}
}
private void Awake()
{
for (var i = 1; i < 11; i++) rangeList.Add(i);
}
then in your SelectingReange()
private void SelectingRange()
{
//....
result = RandomNumber;
SpawnQuestion();
}