• 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 SwathedOrange · May 02 at 08:43 PM · mobilegame developmentrandomizerandomization

What would be your approach to having random categories and each category having more elements?

I am creating a drinking game, with different question types, such as "normal questions", "rule", "chugging" and so on.

What I did is I created a list, for example:

 List<int> listOfQuestions = new List<int> { 1, 1, 2, 3, 3, 1, 1, 1, 1, 1, 1, 1 };

where 1s would be normal questions and if 1 was selected, it would pull out a random question out of a string list containing questions specified for "normal questions", 2s would be rules and 3s would be chugging. Numbers are chosen randomly after each click on the screen and therefore a question resembling that category would appear. I was wondering if there is a better way to do this because as im getting close to finishing the game it feels like this method is too clunky?

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
1

Answer by MickyX · May 03 at 02:39 PM

@SwathedOrange I think I understand what you want. Here is what I would do

Create a new class of Question

 public class Question
 {
     public string description;
     public int type;
 }

I would create a list of that Question class

 public List<Question> questions = new List<Question>();

Populate that list however you want, personally I would use a CSV in resources so I could edit the CSV and the questions would be updated.

To select a random question I would write a function to return a random question based on the type needed. Using Linq to get a question (Note you'll need using System.Linq;)

 public Question GetQuestion(int type)
 {
     List<Question> tempList = (from x in questions where x.type == type select x).ToList();
     return tempList[Random.Range(0, tempList.Count)];
 }


There are a number of benefits to using this, it moves the questions into a external file for easier management, it allows you to add new fields to a question without a clunky string. It also allows for language translation in the external file if you wanted to take the game to other countries.

As an example for expansion I added the ASKED flag.

 public class Question
 {
     public string description;
     public int type;
     public bool asked = false;
 }

Using the new asked bool you can return questions that haven't yet been asked

 List<Question> tempList = (from x in questions where x.type == type && !x.asked select x).ToList();

Its worth noting that that could lead to a list without a selection question due to all questions could have been asked, so you will have to make sure you handle that situation

Something like this, when all questions have been asked I reset all questions of that type

 public Question GetQuestion(int type)
     {
         List<Question> tempList = (from x in questions where x.type == type &&!x.asked select x).ToList();
 
         if(tempList.Count == 0)
         {
             ResetAllQuestions(type);
             tempList = (from x in questions where x.type == type && !x.asked select x).ToList();
         }
 
         Question selectedQuestion = tempList[Random.Range(0, tempList.Count)];
         selectedQuestion.asked = true;
 
         return selectedQuestion;
     }
 
     public void ResetAllQuestions(int type)
     {
         foreach (var question in (from x in questions where x.type== type select x).ToList())
         {
             question.asked = false;
         }
     }


Hope that helps? I haven't tested any of this but should work Thanks Mike

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.

Follow this Question

Answers Answers and Comments

187 People are following this question.

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

Related Questions

Instantiate Prefab at random times but keep 3 from spawning 2 Answers

RNG State Changes When Loop Iteration is Skipped? 0 Answers

Random Sound When Click? 0 Answers

Random objects in coordinates specific 1 Answer

Polybrush prefab rotation increment 0 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