Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by EliteHedgehog56 · Nov 09, 2019 at 06:00 AM · randomweaponpickuppowerup

ramdomised weapon pickup

hey guys, I've got this weapon pickup script set to a pickup object that gives the player a weapon when picked up, so far the pickup only gives one fixed weapon per single pickup and I must create a new pickup for each weapon, so what I want to do is make a mario kart style pickup where it will give a random weapon when picked up instead of each weapon needing it's own pickup. How can I do this ? here's the script

 var MachineGun : GameObject;
 var RocketLauncher : GameObject;
 var Unarmed : GameObject;
 var UsingNothing : boolean = true;
 var Using_MG : boolean = false;
 var Using_RL : boolean = false;
 
 function Start(){
 UsingNothing = true;
 Using_MG = false;
 Using_RL = false;
 
 
 }
 
 
 function OnTriggerEnter (other:Collider){
 
 if (other.GetComponent.<Collider>().gameObject.name == "MachineGun Pickup"){
 
 UsingNothing = false;
 Using_MG = true;
 Using_RL = false;
 
 }
 
 
 
 
 if (other.GetComponent.<Collider>().gameObject.name == "RocketLauncher Pickup"){
 
 UsingNothing = false;
 Using_MG = false;
 Using_RL = true;
 
 }
 
 
 
 
 }
 
 
 function Update(){
 
 if (UsingNothing){
       Unarmed.gameObject.active = true;
       MachineGun.gameObject.active = false;
       RocketLauncher.gameObject.active = false;
 
 }
 
 if (Using_MG){
       Unarmed.gameObject.active = false;
       MachineGun.gameObject.active = true;
       RocketLauncher.gameObject.active = false;
 
 }
 
 
 
 
 
 if (Using_RL){
       Unarmed.gameObject.active = false;
       MachineGun.gameObject.active = false;
       RocketLauncher.gameObject.active = true;
      
 }
 
 
 
 }
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
Best Answer

Answer by CodeMonkeyYT · Nov 09, 2019 at 08:27 AM

You can use Random.Range(min, max); to get a random value https://docs.unity3d.com/ScriptReference/Random.Range.html

So you could do this:

 private void SelectRandomWeapon() {
   int weaponIndex = Random.Range(0, 3);
   switch (weaponIndex) {
   default:
   case 0:
     UsingNothing = true;
     break;
   case 1:
     Using_MG = true;
     break;
   case 2:
     Using_RL = true;
     break;
   }
 }


Not sure why you are using the booleans though, why aren't you activating the weapon game object on the collision?

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 EliteHedgehog56 · May 28, 2020 at 10:41 PM 0
Share

this works great, thanks

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

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

Related Questions

weapon pickup 1 Answer

Make a random number get chosen less or more? (C#) 1 Answer

How can I detect if box is touching a tag named weapon? 1 Answer

PickUp Weapon in Brick Breaker Game 1 Answer

Weapon pick up 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