Please help me on how i randomize the id of my questions in json file.
What part of this code can I put the code for the random.
here is my code:
using UnityEngine;
using System.Collections;
using LitJson;
using UnityEngine.UI;
using System.IO;
public class Question : MonoBehaviour {
public string filePath;
public string jsonString;
public JsonData questionData;
public int numberQuestion=0;
public GameObject answerPrefab;
public bool nextQuestion;
public bool clickAnswer;
public int score;
public void QuestionBegin(string jsonName){
score = 0;
nextQuestion = true;
//filePath = (Application.dataPath + jsonName);
//TextAsset file = Resources.Load(filePath) as TextAsset;
//Debug.Log (file);
//jsonString = file.ToString ();
//jsonString = File.ReadAllText(jsonName);
filePath = System.IO.Path.Combine (Application.streamingAssetsPath, jsonName + ".json");
StartCoroutine ("Json");
questionData = JsonMapper.ToObject(jsonString);
OnClick ();
}
IEnumerator Json(){
if (filePath.Contains ("://")) {
WWW www = new WWW (filePath);
yield return www;
jsonString = www.text;
}
else{
jsonString = System.IO.File.ReadAllText (filePath);
}
}
public void OnClick (){
if(numberQuestion >= questionData["data"].Count){
Debug.Log ("Result of");
if (score == questionData ["data"].Count) {
GameObject.Find ("RankMessage").GetComponent<Text> ().text = "VERY GOOD";
}else
if (score >= questionData ["data"].Count*1/2) {
GameObject.Find ("RankMessage").GetComponent<Text> ().text = "GOOD";
}else
if (score <= questionData ["data"].Count*1/2) {
GameObject.Find ("RankMessage").GetComponent<Text> ().text = "FAILED";
}
MenuManager menuResult = GameObject.Find("Canvas").GetComponent<MenuManager>();
menuResult.ShowMenu (GameObject.Find("Result").GetComponent<Menu>());
GameObject.Find ("Score").GetComponent<Text> ().text = score.ToString () + "/" + questionData ["data"].Count;
}
if (nextQuestion) {
GameObject[] answerDestroy = GameObject.FindGameObjectsWithTag("Answer");
if (answerDestroy != null) {
for (int x = 0; x < answerDestroy.Length; x++) {
DestroyImmediate (answerDestroy [x]);
}
}
GameObject.Find ("ShortQuiz1/Panel/QuestionContent/Question").GetComponentInChildren<Text> ().text = questionData ["data"] [numberQuestion] ["question"].ToString ();
for (int i = 0 ; i < questionData ["data"] [numberQuestion] ["answer"].Count; i++) {
GameObject answer = Instantiate (answerPrefab);
answer.GetComponentInChildren<Text> ().text = questionData ["data"] [numberQuestion] ["answer"] *.ToString ();*
-
Transform answerContent = GameObject.Find ("AnswerContent").GetComponent<Transform> ();*
-
answer.transform.SetParent (answerContent);*
-
string x = i.ToString ();*
-
if (i == 0) {*
-
answer.name = "Correct Answer";*
-
answer.GetComponent<Button> ().onClick.AddListener (() => Answer ("0"));*
-
} else {*
-
answer.name = "Wrong Answer" + x;*
-
answer.GetComponent<Button> ().onClick.AddListener (() => Answer (x));*
-
}*
-
answer.transform.SetSiblingIndex (Random.Range (0, 3));*
-
}*
-
numberQuestion++;*
-
nextQuestion = false;*
-
clickAnswer = true;*
-
StartCoroutine ("Timer");*
- }*
}
-
public void Answer(string x){*
-
if (clickAnswer) {*
-
if (x == "0") {*
-
score++;*
-
GameObject.Find ("Correct Answer").GetComponent<Button> ().image.color = Color.green;*
-
GameObject.Find ("Image (" + numberQuestion + ")").GetComponent<Image> ().color = Color.green;*
-
Debug.Log ("Correct Answer");*
-
} else {*
-
GameObject.Find ("Wrong Answer" + x).GetComponent<Button> ().image.color = Color.red;*
-
GameObject.Find ("Correct Answer").GetComponent<Button> ().image.color = Color.green;*
-
GameObject.Find ("Image (" + numberQuestion + ")").GetComponent<Image> ().color = Color.red;*
-
Debug.Log ("Wrong Answer");*
-
}*
-
}*
-
nextQuestion = true;*
-
clickAnswer = false;*
-
}*
-
IEnumerator Timer(){*
-
Image time = GameObject.Find ("Timer").GetComponent<Image>();*
-
time.fillAmount = 1;*
-
float timeToWait = 5f;*
-
float incrementToRemove = 0.05f;*
_ float x = time.fillAmount / timeToWait * incrementToRemove;_
-
while (timeToWait > 0) {*
-
yield return new WaitForSeconds (incrementToRemove);*
-
if (!nextQuestion) {*
-
time.fillAmount -= x;*
-
timeToWait -= incrementToRemove;*
-
}else{*
-
timeToWait = 0;*
-
}*
-
if(time.fillAmount <= 0.01f){*
-
for(int i=1; i<4; i++){*
-
GameObject.Find ("Wrong Answer" + i ).GetComponent<Button> ().image.color = Color.red;*
-
}*
-
GameObject.Find ("Correct Answer").GetComponent<Button> ().image.color = Color.green;*
-
GameObject.Find ("Image (" + numberQuestion + ")").GetComponent<Image> ().color = Color.red;*
-
clickAnswer = false;*
-
nextQuestion = true;*
-
}*
-
}*
-
}*
}
thank you!