• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by khelvino · Feb 16, 2017 at 02:59 AM · databasejsonid

How am I Randomize my id in json file.

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"] [i].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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Powsha29 · Sep 12, 2017 at 10:57 AM

I have a same problem as yours, have you managed to fix it?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a user defined ID for firebase realtime database 0 Answers

Dynamically load json data to a game: What are the best practices? 1 Answer

Converting from SQLite Database to JSON 2 Answers

Calling Json script and displaying the Images within the script 0 Answers

Json.net deserialization problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges