Advertisement
aweu

Shuffle

Jan 23rd, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class QuizManager : MonoBehaviour
  7. {
  8.     public float timeRandom;
  9.     TextMeshProUGUI textSoal,textTime,textA, textB, textC, textD;
  10.     //private int random;
  11.     public List<Quest> quest;
  12.  
  13.     [System.Serializable]
  14.     public class Quest
  15.     {
  16.         public string soal;
  17.         public string pilA, pilB, pilC, pilD;
  18.         public bool A, B, C, D;
  19.     }
  20.  
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         textTime = GameObject.Find("Time Text").GetComponent<TextMeshProUGUI>();
  25.         textSoal = GameObject.Find("QuestionText").GetComponent<TextMeshProUGUI>();
  26.         textA = GameObject.Find("A").GetComponent<TextMeshProUGUI>();
  27.         textB = GameObject.Find("B").GetComponent<TextMeshProUGUI>();
  28.         textC = GameObject.Find("C").GetComponent<TextMeshProUGUI>();
  29.         textD = GameObject.Find("D").GetComponent<TextMeshProUGUI>();
  30.  
  31.         //random = Random.RandomRange(0, quest.Count);
  32.        
  33.         Shuffle(quest);
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         textTime.text = "" + timeRandom.ToString("0");
  40.  
  41.         timeRandom -= Time.deltaTime;
  42.         if (timeRandom <= 0)
  43.         {
  44.             quest.RemoveAt(random);
  45.             timeRandom = 5;
  46.             random = Random.RandomRange(0, quest.Count);
  47.         }
  48.  
  49.         textSoal.text = quest[random].soal;
  50.         textA.text = quest[random].pilA;
  51.         textB.text = quest[random].pilB;
  52.         textC.text = quest[random].pilC;
  53.         textD.text = quest[random].pilD;
  54.     }
  55.  
  56.     void Shuffle<T>(List<T> list)
  57.     {
  58.         System.Random random = new System.Random();
  59.         int n = list.Count;
  60.         while (n > 1)
  61.         {
  62.             int k = random.Next(0, n);
  63.             n--;
  64.             T temp = list[k];
  65.             list[k] = list[n];
  66.             list[n] = temp;
  67.         }
  68.     }
  69.  
  70.     /*for (int Quest = 0; Quest < quest.Count; Quest++)
  71.         {
  72.             T temp = quest[Quest];
  73.             int random = Random.Range(0, quest.Count);
  74.             quest[Quest] = quest[random];
  75.             quest[random] = temp;
  76.         }*/
  77.  
  78.     /*public void Correct(string answer)
  79.     {
  80.         if (Quest[random].A==true && answer=="a")
  81.         {
  82.             PlayerController.isSafeOpen = true;
  83.         }
  84.         if (Quest[random].B==true && answer=="b")
  85.         {
  86.             PlayerController.isSafeOpen = true;
  87.         }
  88.         if (Quest[random].C==true && answer=="c")
  89.         {
  90.             PlayerController.isSafeOpen = true;
  91.         }
  92.         if (Quest[random].D==true && answer=="d")
  93.         {
  94.             PlayerController.isSafeOpen = true;
  95.         }
  96.         //Quest.RemoveAt(random);
  97.         //random = Random.RandomRange(0, Quest.Count);
  98.     }*/
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement