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 /
  • Help Room /
avatar image
0
Question by zigglr · Nov 19, 2015 at 12:03 PM · c#unity 5

How to choose a random preference (complicated)

Before the game starts, the player has 5 preferences, a b c d and e, and he can enable and disable as many as he wants. For example

a - enabled b - disabled c- enabled d - enabled e - disabled

When the player enters a trigger, a random preference (a b c d or e) is chosen. If the preference is disabled, this preference won't be put into the random choice. Since there are so many different combinations the user can have, is there a simple way to do this other than writing out every single possible combination that the user may have chosen? I know I will use ontriggerenter and Random.Range, but not sure where to go from there

Thanks

Comment
Add comment · Show 1
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 NoseKills · Nov 19, 2015 at 05:25 PM 0
Share

I'm not sure i understand your problem correctly.

You have let's say 5 options A,B,C,D,$$anonymous$$ Let's say they are all represented by gameObjects.

First the player can disable any of them. After that he enters the trigger of one of the still enabled objects, and at that point you want to get a list of all the other still enabled objects ?

Is that it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KdRWaylander · Nov 19, 2015 at 12:16 PM

Hi,

On trigger enter, you want your app to compute 5 times a little routine to set randomly your preferences ? Are those references booleans ? Or ints/floats ?

If it's a boolean choice, here's a way to do this:

  • if (preference is disabled) → return;

  • else: throw a random value

  • and : if random value is > to .5f set preference to true, else set to false

  • apply new value to preference

Now you loop in all your preferences with a foreach :)

If it's not a boolean choice, but floats, use random.range and set the output value to your preference or make a switch case on a random.range with integers

Comment
Add comment · Show 3 · 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 zigglr · Nov 19, 2015 at 12:21 PM 1
Share

Hey thanks for you answer, I decided to add the enabled options to a list at the start and then choose a random one from the list

avatar image KdRWaylander zigglr · Nov 19, 2015 at 01:07 PM 0
Share

Problem solved then ?

avatar image saschandroid zigglr · Nov 19, 2015 at 02:21 PM 0
Share

If you just want a random bool-combination for a, b, c, d, and e you can use something like this:

 byte m_combination;
     bool a;
     bool b;
     bool c;
     bool d;
     bool e;
     public void RandomCombination()
     {
         m_combination = (byte)Random.Range(0, 32);
 
         if ((m_combination & (byte)1) == (byte)1) a = true;           
         else  a = false;
 
         if ((m_combination & (byte)2) == (byte)2) b = true;
         else b = false;
 
         if ((m_combination & (byte)4) == (byte)4) c = true;
         else c = false;
 
         if ((m_combination & (byte)8) == (byte)8) d = true;
         else d = false;
 
         if ((m_combination & (byte)16) == (byte)16) e = true;
         else e = false;
         //Debug.Log(a + " " + b + " " + c + " " + d + " " + e);
     }


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to carry over button sound to another scene 1 Answer

NullReferenceException: Error from the GetStyle() - Method 1 Answer

Rayscast from the GameObject that is not the player does not work 0 Answers

UnityAction Vs UnityEvent 1 Answer

Error `UnityEngine.Component' does not contain a definition 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