• 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

2 Replies

· Add your reply
  • Sort: 
avatar image
20
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 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 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
18

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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 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

Related Questions

Spawn help, 4 spawners but need one object on screen at a time 1 Answer

Spawning objects at 3 spawn points randomly one at a time. 1 Answer

Object Spawning Randomly 0 Answers

i want to spawn randomly 1 Answer

Percentage for random 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges