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 /
avatar image
0
Question by Sun_Glasses_Guy · Jul 31, 2011 at 03:27 AM · terrainvariablerandomfloat

Random Float Variable

This is horribly frustrating, maybe I'm just doing it wrong. I want to spawn a random(pre-made) object at an empty gameobject. I think that the easiest way to do this would be to call a random float number, like; 1-5. And then I'd use an if function to check the variable, and spawn the object if it's the right number.

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
1
Best Answer

Answer by aldonaletto · Jul 31, 2011 at 04:20 AM

You can use Random.Range(0,100) and compare it to the percentage limit:

  if (Random.Range(0, 100) < 20){
    var clone = Instantiate(...);
  }
The chance to instantiate the object in this example is 20%
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 DaveA · Jul 31, 2011 at 04:58 AM 0
Share

That will give you integers, if you want floats use Random.Range(0.0,100.0), but really how is it different from 1,5 like he suggests? He can use Random.value < 0.2 same.

avatar image aldonaletto · Jul 31, 2011 at 12:08 PM 0
Share

The results would be the same, indeed, but I prefer to use Random.Range(0, 100) in my answers because it clearly resembles percentage, an intuitive concept for everybody. The normalized range 0..1 is more intuitive for us, technical people, but it's not so clear for surprisingly many people. And comparing Range(1,5) would fail if Range fell in the float version, what could occur if someone decided to use a float variable as one of the parameters ($$anonymous$$urphy's law...). But I had the same doubt as you: 5 objects to be drawn, or one object with 20% chance?

avatar image Sun_Glasses_Guy · Jul 31, 2011 at 04:55 PM 0
Share

Hey Thanks! It works like a charm, I'm using it for terrain generation just so you know. I can't tell you how amazing it is to see trees pop up in different places all over the terrain whenever I click the play button!

I can't thank you enough!

avatar image
0

Answer by DaveA · Jul 31, 2011 at 04:59 AM

Your question is ambiguous, can you explain more? Do you want to spawn ONE type of object at ONE place, on a 20% chance of doing so? If so, just compare Random.value to 0.2, if it's less, spawn away. Or do you want to spawn ONE type of an object at one of FIVE possible places? If so, you can have an array of 5 empty game objects and use that INTEGER to pick an element (0 to 4 by the way), and parent the new object to the chosen empty object. Or just assign the new object's position same as the chosen empty object.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Setting float variable to an Array value 1 Answer

Spawn Random Enemy 3 Answers

Check if var is true from a random object 0 Answers

Check increasing float variable 1 Answer

The type or namespace name `timeLeft' could not be found. Are you missing a using directive or an assembly reference? 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