Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Muhammad-Owais · Apr 09, 2015 at 04:42 PM · c#randompercentagechance

probability of child Game objects

Please anybody can help me on probability of child Game objects

I want this type of snareo child (0) = 20% chance, child (1) = 30% chance, child (2) = 10% chance, child (3) = 40% chance,

I am using this code

 timer += Time.deltaTime;
         if(timer>crate_timer)
         {
             int rndm = Random.Range(0,6); // here I want probablity not randomness
             for (int i=0; i<transform.childCount; i++)
             {
                 transform.GetChild(rndm).gameObject.SetActive(true);
             }
 
             timer = 0;
         }
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 meepong · May 26, 2015 at 06:25 AM 0
Share

$$anonymous$$ay I know the solution for this answer? I am having the same problem

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Apr 09, 2015 at 04:53 PM

The way I handle this is to create a temporary list containing multiples of the child indices where each entry in the list is worth 1% point (so 100 entries). For instance, if you have two children, and you want the first child to be chosen 75% of the time:

 List<int> indices = new List<int>();

 // Add the child 0 element 75 times
 for (int i=0; i < 75; ++i)
     indices.Add(0);

 // Add the child 1 element 25 times
 for (int i=0; i < 25; ++i)
     indices.Add(1);

 int ranVal = Random.Range(0, indices.Count);
 int childIndex = indices[ranVal];
 transform.GetChild(childIndex).gameObject.SetActive(true);

Obviously you would not need to add 100 items in this case, because the ratio is 1 to 3, so you could indeed just add three "0"s and a single "1" into the indices list. Then you'd have 1 in 4 chances to get a 1 and 3 in 4 chances to get a 0 (where 1 and 0 are the index into the child transform array).

If you're simulating a bag, removing an item (indices.RemoveAt(ranVal)) decreases the chance of getting that item next time, so you could also remove the index from the indices list. So if you pull out a 0, all that is left in the list are 1's. In this case, you must add the exact number of elements you plan to have in the bag (you can't use 75 and 25 just because they nicely add to 100%, unless you explicitly have 100 items in the bag).

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 Muhammad-Owais · Apr 14, 2015 at 08:28 AM 0
Share

Thankx supernat I figured it out , but that is very good answer. Thankx again

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

Trouble with Random.value and Percentage 1 Answer

Clean way for percentages 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Spawn chance in my spawner. 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