Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by rottweiler · Jan 22, 2013 at 06:02 PM · randomspawningpercent

How to spawn prefabs with percent random?

I need to instantiate 3 different prefabs.

First prefab must spawn with 50 %

Second prefab must spawn with 30 %

Third prefab must spawn with 20 %

I know how to intantite objects, but i didnt know how to make percent random.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by jogo13 · Jan 22, 2013 at 06:10 PM

Random.value returns a float between 0 to 1. You can use it to generate percentage random like so:

 if(Random.value > 0.5) //%50 percent chance
 {//code here
 }
 
 if(Random.value > 0.2) //%80 percent chance (1 - 0.2 is 0.8)
 { //code here
 }
 
 if(Random.value > 0.7) //%30 percent chance (1 - 0.7 is 0.3)
 { //code here
 }
Comment
Add comment · Show 4 · 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 jister · Apr 16, 2014 at 04:55 PM 0
Share

what if it returns 0.6, then it would run the first and the second if statement...?

avatar image quantumface · Jun 16, 2015 at 03:51 AM 0
Share

Nope. On each of those if statements a new Random.value is generated. It would work as you've thought only if a variable has been set to be Random.value once, and then re-used in all those if statements.

avatar image GarMatt_ · May 31, 2020 at 05:22 PM 0
Share

Sorry for the necroposting here but isn't there a chance that it doesn't instantiate with that code?

avatar image ms11153 · Dec 03, 2021 at 08:05 PM 0
Share

I'm using this code for a chance of either having a health potion be dropped, or the player taking damage. Sometimes they both happen, or twice of one or another happen. Is there any way to fix this?

avatar image
20

Answer by Kalbytron · Sep 09, 2013 at 11:15 PM

You can also say

if(Random.value <= 0.2)

to get a %20 chance without the confusion.

Comment
Add comment · Show 4 · 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 ofuscapreto · Jun 22, 2017 at 12:41 AM 0
Share

I think this is the best way, thanks!

avatar image DigitalCyclone · Jan 05, 2019 at 03:53 PM 0
Share

Wouldn't it be if(0.2 > Random.value)?

for example, say the random value generated a 30%

20% is NOT greater than 30%, therefore the if statement returns false.

however, when the random value generates something like a 15%

20 IS greater than 15, therefore the code will run.

avatar image David_Rios DigitalCyclone · Jun 18, 2019 at 10:18 PM 0
Share

That's the same thing as saying if(Random.value < 0.2) @DigitalCyclone; the <= is just to get the .20 along with all of the other values that are below it.

avatar image Armetron · Aug 29, 2020 at 09:04 PM 0
Share

problem with this is random.value is 0 to 1 [INCLUSIVE] which means that it is possible for the value to be 0, if the chance value was set to 0.0 which means it should never happen since your using <= then there is a slight chance that it could fire.

avatar image
0

Answer by unity_973D59F5B05CFF0E9822 · Aug 01, 2021 at 07:26 AM

 public float Chance()
 {
     float[] chances = new float[] { 0.5f, 0.3f, 0.2f }; // Your chances. Summary = 1.0f
 
     float randValue = UnityEngine.Random.value; // Randome value (chance value)
 
     float currentChance = 0f;
     float minChanceRange;
     float maxChanceRange;
 
     for (int i = 0; i < chances.Length; i++)
     {
         currentChance += chances[i];
 
         minChanceRange = 0.5f - currentChance / 2;
         maxChanceRange = 0.5f + currentChance / 2;
 
         if (randValue >= minChanceRange && randValue <= maxChanceRange)
         {
             return chances[i]; //code here
         }
     }
 
     return -1; //This code unreacheble
 }
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

20 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

Related Questions

How do I make a game object spawn several times in semi-random locations? 1 Answer

I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line 1 Answer

Weighted Random Spawning 1 Answer

Enemies spawn on top of each other(C#)(Unity) 1 Answer

How do I randomly create 2 objects, each with a different shape and color? 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