• 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 Theacesofspades · Oct 29, 2012 at 11:25 PM · fpsparentingweapons

Weapon Switching Help

Hey im making a fps and i have all of my weapons and i have an in game pause menu to where you can buy weapons in a store. Im new to using child/parenting functions so im not sure on where to start with my coding. What i am trying to do is when a var is true it selects a certain child of the weapons game object and deselects the other when you press either 1 or 2, 1 being the primary weapon and the 2 being the secondary. Im trying to use this code but its not working how i want it to.

 static var beretta = true;
 static var deagle = false;
 static var revolver = false;
 static var m3 = false;
 static var mp5 = false;
 static var mp7 = true;
 static var spaz = false;
 static var trench = false;
 static var toyshotgun = false;
 var weapon : int;
 
 
 
 
 
 
 function Start () 
 {
     weapon = 0;
 }
 
 function Update () 
 {
     
     if (Input.GetKeyDown("1")) 
     {
         if (m3)
         weapon = 1;
         
         if (mp5)
         weapon = 2;
         
         if (mp7)
         weapon = 3;
         
         if (spaz)
         weapon = 4;
         
         if (trench)
         weapon = 5;
         
         if (toyshotgun)
         weapon = 6;        
         
     }
     if (Input.GetKeyDown("2")) 
     {
         if (beretta)
         weapon = 0;//
         
         if (deagle)
         weapon = 7;
         
         if (revolver)
         weapon = 8;    
         
     }
 }
 
 function SelectWeapon (index : int) 
 {
     for (var i=0;i<transform.childCount;i++)    
     {
         if (i == index)
             transform.GetChild(weapon).gameObject.SetActiveRecursively(true);
         else
             transform.GetChild(weapon).gameObject.SetActiveRecursively(false);
     }
 }
Comment
Add comment · Show 1
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 aldonaletto · Oct 29, 2012 at 11:30 PM 0
Share

Unity Answers ate your code after the < character in SelectWeapon - it "thinks" this character is a tag and skip all the text ahead until a closing > is found. Edit your question and post the code again - but this time skip a blank line after the question text, select the code and press ctrl-$$anonymous$$ to format it

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Oct 30, 2012 at 05:27 AM

is every weapon a child of the player, even ones they dont own? thats wierd. That is by the way what your code assumes.

 for (var i=0;i<transform.childCount;i++) 

if they own 1 weapon the childcount might only be equal to 1. meaning that for loop will stop after the first thing.

change the code up

 if(m3)
 getcomponent<M3>();
 //do what you want with the M3
 if(berreta)
 getcomponent<berreta>();
 //do what you want with the berreta

each gun should have a script each script should be the name of the gun

your probably thinking, but i want to call fire on all these weapons but have fire do different things depending on the weapon. That means you want a base class called weapon and then subclasses that inherit from it and override the base methods.

for example

 class weapon()
 {
 public
 bool fire();
 
 }
 
 bool weapon.fire(){
 
 //do nothing now, your just creating a base so you can override later
 return true;
 }
 
 class weapon::M3()
 {
 public
 bool fire();
 }
 
 bool weapon::M3 fire()
 {
 fire as is appropriate for an M3
 }

you do that for each weapon

now it is both a weapon and an M3 (polymorphism)

now you can create a weapon and call fire and it will do different things depending on the type of weapon it is.

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 Theacesofspades · Oct 30, 2012 at 09:48 PM 0
Share

yes i do have them all as a child of a GameObject called weapons which is a GameObject of the main camera. What i am asking is how do i enable a certain child and disable all the other children if a variable is true?

avatar image sparkzbarca · Oct 30, 2012 at 11:16 PM 0
Share

there not a bool are they? what do you mean enable and disable, they are game items. You can for example create a weapons variable and change which weapon is assigned to it.

What does enabling mean in context? it means you send commands to that specific weapon? well create an empty weapon variable and assign weapons to the variable and do things with the variable.

Variables are so things can vary, you want a fire command on a weapon but you want the weapon to vary. you want a weapon class so you can make a weapon variable in your scrip.t

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

11 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

Related Questions

Projectile instantiation positioning is off. 3 Answers

Making weapons animations for first person shooter 2 Answers

Weapon switching using array 1 Answer

Field of View Morphing weapons 1 Answer

Where is the FPS Tutorial? 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