Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 HazyLV · Jan 31, 2017 at 08:36 PM · liststringrandom.range

How do I find if a string from list was used and exclude it from further use.

I am making a quiz game in Unity 2d and it's almost complete (excluding all the questions, but that's not important). All my questions and answers are in a List, they are in "questions" List and "correctAnswer" List. The questions are randomized and i want to make it that if a question is picked by random at least once, then it won't be used again, meaning it won't be used by the randomizer.

Sorry if i used something incorrectly, i'm a beginner. What is in questions is not important it's in another language. P.S. I am looking for how to make random range not to repeat.

  List<string> questions = new List<string>() { "Pasaules augstākais kalns?", "Kas ir Vācijas galvaspilsēta?", "Kur atrodas ASV?", "Geo jautājums 4", "jt", "Geo jautājums 5", "Cik ir 2+2?", "Cik ir 12+1?", "Cik ir 9+10?", "Kvadrātsakne no 49", "2+2=5?" };
 
     List<string> correctAnswer = new List<string>() { "3", "1", "2", "3", "1", "2", "2", "3", "1", "1" };
 
     public static string selectedAnswer;
 
     public Transform resultObj;
 
     public static string choiceSelected = "n";
 
     public static int randQuestion = -1;
 
     public int catMod = 0;
 
     public Transform auraObj;
 
     public float totalCorrect = 0;
 
     public float totalQuestions = 0;
 
     public Transform scoreObj;
 
     public float scorePer;
 
     public Transform nextButton;
 
     public Transform firstText;
     public Transform secondText;
     public Transform thirdText;
 
     public Transform firstChild;
     public Transform secondChild;
     public Transform thirdChild;
 
 
 
     void Start()
     {
         if (selectCat.catTopic == "Matemātika")
         {
             catMod = 5;
         }
 
         if (selectCat.catTopic == "Ģeogrāfija")
         {
             catMod = 0;
         }
     }
 
     void FixedUpdate()
     {
 
         //scoreObj.GetComponent<TextMesh>().text = "Score  :  " +totalCorrect;   nextButton.GetComponent<Transform>().position = new Vector3(28.8f, 8.62f, 0);
 
 
         if (totalQuestions > 0)
         {
             scorePer = (totalCorrect / totalQuestions) * 100;
         }
 
 
         scoreObj.GetComponent<TextMesh>().text = "Score  :  " + scorePer+ "%";
         if (randQuestion == -1)
         {
             randQuestion = Random.Range(0 + catMod, 5 + catMod);
 
             nextButton.GetComponent<Transform>().position = new Vector3(49.1f, 8.62f, 0);
 
             firstText.GetComponent<BoxCollider2D>().offset = new Vector2(-0.1f, 0.2f);
             secondText.GetComponent<BoxCollider2D>().offset = new Vector2(-0.1f, 0.2f);
             thirdText.GetComponent<BoxCollider2D>().offset = new Vector2(-0.1f, 0.2f);
 
             firstChild.GetComponent<BoxCollider2D>().offset = new Vector2(0.05347621f, -5.174473f);
             secondChild.GetComponent<BoxCollider2D>().offset = new Vector2(0.05347621f, -5.174473f);
             thirdChild.GetComponent<BoxCollider2D>().offset = new Vector2(0.05347621f, -5.174473f);
 
 
 
 
         }
 
         if (randQuestion > -1)
         {
             GetComponent<TextMesh>().text = questions[randQuestion];
             
         }
 
         //Debug.Log(questions[randQuestion]);
 
         if (choiceSelected == "y")
         {
             choiceSelected = "n";
             totalQuestions += 1;
 
 
             if (correctAnswer[randQuestion] == selectedAnswer)
             {
                 resultObj.GetComponent<TextMesh>().text = "Pareizi! Ejiet pa labi lai turpinātu";
                 totalCorrect += 1;
                 nextButton.GetComponent<Transform>().position = new Vector3(28.8f, 8.62f, 0);
                 firstText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
                 secondText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
                 thirdText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
 
                 firstChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
                 secondChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
                 thirdChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
 
             }
 
             else
             {
                 NextButton.resetAura = "n";
                 resultObj.GetComponent<TextMesh>().text = "Nepareizi!";
 
                 if (correctAnswer[randQuestion] == "1")
                 {
                     auraObj.GetComponent<Transform> ().position = new Vector3(-3.4f, 24.2f, 0);
                 }
 
                 if (correctAnswer[randQuestion] == "2")
                 {
                     auraObj.GetComponent<Transform>().position = new Vector3(9.9f, 24.2f, 0);
                 }
 
                 if (correctAnswer[randQuestion] == "3")
                 {
                     auraObj.GetComponent<Transform>().position = new Vector3(22.9f, 24.2f, 0);
                 }
 
                 nextButton.GetComponent<Transform>().position = new Vector3(28.8f, 8.62f, 0);
                 firstText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
                 secondText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
                 thirdText.GetComponent<BoxCollider2D>().offset = new Vector2(50f, 0.2f);
 
                 firstChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
                 secondChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
                 thirdChild.GetComponent<BoxCollider2D>().offset = new Vector2(50f, -5.174473f);
             }
         }
 
        
     }
 }


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 elenzil · Jan 31, 2017 at 09:07 PM

i'd suggest making a copy of the list, call it "unusedQuestions" or something, and when you choose an item from it at random, remove that item from the list.

Comment
Add comment · Show 1 · 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
avatar image juicyz · Feb 01, 2017 at 01:02 AM 0
Share

You could do the above mention and that would work or if you really care about memory (which you probably won't need to worry about..) you could do the following:

You could keep track of how many questions you have with an int that would be used in the random number generation. Everytime a question is picked, swap that one with the one in the last position of the list and decrease the limit of the int used in the random number generator by one.

If you have further questions about this, reply back and I can go more into detail.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Update List<> in editor 0 Answers

Is it possible to name a list by a string variable? 2 Answers

Randomize a spawn position without repition 3 Answers

How do I make a list of lists? 2 Answers


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