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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by CrilleStyles · Jun 10, 2014 at 01:24 PM · randomnoobrandomizeprefabe

How do I randomize 5 prefabs on Scene loaded?

I just woder how do I randomize for example 5 prefabs: Thunderbolt, Mackey x270, CM lazer, Pashasprayer, Badass shotgun? I want it to be 50% chance of thunderbolt 15% chance of Mackey x270 5% chance of pashasprayer 29% chance of badass shotgun and 1% chance of CM Lazer? I just want them equipped with a random scrip, don't care wich one. I'm just gonna use one gun.

Thank you for your time

Comment
Add comment · Show 3
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 Landern · Jun 10, 2014 at 01:44 PM 1
Share

It's possible someone might come along and write something for you, you will get better responses if you at least attempt to create the code yourself... if you do so, post it here with any issues and you will probably get some advise.

avatar image CrilleStyles · Jun 10, 2014 at 01:58 PM 0
Share

I'm still not that good with coding, Pretty basic. I'm just asking here to get a clue about what to write

avatar image Landern · Jun 10, 2014 at 02:00 PM 0
Share

Well, i would suggest you take a stab at it first, this is how most of us have learned, through trial and error..error...error, win?

you already know you need at least 5 prefabs, which means you need an collection or array of either GameObjects/Transforms to be set in the inspector.

The name can be set on the game object if you want. the rest is just math and association.

1 Reply

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

Answer by Nick4 · Jun 10, 2014 at 02:21 PM

I'm not good at calculating possibilities but let me take a shot.

 public class Class1 : MonoBehaviour
 {
     // Assign your prefabs to this array in the inspector panel
     public GameObject[] guns = new GameObject[5];
 
     // Weapon that will be assigned to, after required calculations
     private GameObject currentWeapon;
 
     void Start()
     {
         currentWeapon = RandomWeapon()
     }

     // Returns a random weapon based upon a random int variable.
     private GameObject RandomWeapon()
     {
         int random = Random.Range(1, 100);
 
         if(random <= 50)
         {
             return SelectWeapon("thunderbolt");
         }
 
         else if(random > 50 && random <= 65)
         {
             return SelectWeapon("mackey");
         }
 
         else if(random > 65 && random <= 70)
         {
             return SelectWeapon("sprayer");
         }
 
         else if(random > 70 && random <= 99)
         {
             return SelectWeapon("shotgun");
         }
 
         // which leaves us the remaining %1, 100.
         else
         {
             return SelectWeapon("cmLazer");
         }
     }
 
     // Finds and returns the requested weapon by name
     private GameObject SelectWeapon(string weaponName)
     {
         for(int i = 0; i < guns.Length; i++)
         {
             if(guns[i].name == weaponName)
             {
                 return guns[i];
             }
         }
     }
 }

I'm sorry if there are typos in my code because I have directly written these here. Note that it's probably not an efficient way to return a random variable from an array based on given percentages. I just wanted to write some code.

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 Landern · Jun 10, 2014 at 02:23 PM 1
Share

Enabler :)

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

23 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 avatar image avatar image avatar image

Related Questions

i need help with a random number health script 2 Answers

Random number that doesn't repeat. (javascript) 3 Answers

Instantiate Prefab at random times but keep 3 from spawning 2 Answers

Random Generation Algorithm Error 3 Answers

Uhhhh... 2D Maze Generator Help! 3 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