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 James 7 · Nov 09, 2010 at 08:56 PM · variablerandomnumbergenerator

RNG output to a variable + instantiating a prefab based on said variable?

I'm working on a platforming game and what I'd like to do is to create a series of levels which are more or less, random.

What I'm looking for is a script that will choose a variable between a set of numbers, say 1 and 3. Based on the variable selected, it will then instantiate a prefab block or trap on the game object it is attached to. Where I can choose the prefab referenced by each variable on a case by case basis, to prevent getting 10 spike blocks in a row, etc.

I've little javascript experience and I can find RNG scripts that work. I'm just not sure how to output them to a variable and how to reference that variable to instantiate a certain object in my level. I apologize if anything doesn't make any sense, I'm not exactly sure how to phrase a lot of the question. But simply, does anyone have an idea where I can find this script or how I may go about creating it myself?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jesse Anders · Nov 09, 2010 at 11:36 PM

To generate a random integer in a specific range, you can use Random.Range(); keep in mind that the number returned will be in the range [min, max). You can then create an array of game objects, assign references to prefabs to the elements of the array, and choose one of the prefabs based on the random number returned.

If you want to ensure that the distribution has particular characteristics (e.g. not getting the same item 10 times in a row), then you may need to use another method to generate the random index. One method would be to apply a 'random shuffle' algorithm to an array of indices with the desired distribution, and then grab indices from this array as needed (you may want to reshuffle the array whenever you get to the end). I don't think the NET libraries include a 'random shuffle' function, but IIRC, andeeee on the forums posted some 'randomization' utility scripts that include such a function.

Comment
Add comment · Show 2 · 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 James 7 · Nov 10, 2010 at 12:07 AM 0
Share

Well what I was hoping for was a script I could apply to multiple empty game objects which allowed me to pick which prefabs would be referenced for each separate object.

Ex. Write the script and place it on Tile 1. Tile 1 can be a block, trap, or empty. Apply the same script to Tile 2, except Tile 2 can only be a block or a trap, where it's more likely to be a block than a trap(2/3 of the variables reference the block).

avatar image Jesse Anders · Nov 10, 2010 at 07:11 AM 0
Share

You could achieve this by creating a script with a public array of game objects, and then choosing one randomly as I described above. You would then assign references to prefabs to the array elements as desired; to give specific prefabs extra 'weight', you would simply add them more than once. (There are other ways to do it, but that would probably be the most straightforward way.)

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

No one has followed this question yet.

Related Questions

Randomly generated number 0 Answers

Random.Range(..) not working 1 Answer

ArgumentException: RandomRangeInt can only be called from the main thread. 1 Answer

Lottery system 1 Answer

Generate 8 unique random integers 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