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
1
Question by Marsallima · Nov 24, 2011 at 10:29 PM · animationfpsfunctiongunweapon

Select Object ( Weapon ) Quick! Help! xD

Hi guys :D, how i make to i can change the SelectWeapon to for example "SelectObject ("M4A1")" ?, My script:

function Start () { SelectWeapon(0); }

function Update () { // Did the user press fire? if (Input.GetKeyDown("1")) { SelectWeapon(0); }
else if (Input.GetKeyDown("2")) { SelectWeapon(1); } }

function SelectWeapon (index : int) { for (var i = 0; i < transform.childCount; i++) { // Activate the selected weapon if (i == index) transform.GetChild(i).gameObject.SetActiveRecursively(true); // Deactivate all other weapons else transform.GetChild(i).gameObject.SetActiveRecursively(false); } } Thanks.

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

Answer by aldonaletto · Nov 25, 2011 at 12:16 AM

Supposing you want to select the weapon by name, the code below can do the job. It's basically the same thing, but more optimized: check all weapons childed to this object and compare its name to the name specified, using the boolean result to set active true or false:

function SelectWeapon (weaponName: String){
    // check all weapons childed to transform
    for (var weapon in transform){
       // only the weapon with the specified name will be activated:
       weapon.gameObject.SetActiveRecursively(weapon.name == weaponName);
    }
}
Comment
Add comment · Show 6 · 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 Marsallima · Nov 25, 2011 at 01:01 AM 0
Share

Can you re post you script organized? please xD

avatar image aldonaletto · Nov 26, 2011 at 01:29 PM 0
Share

Dear God, what a noob mistake! Shame on me! Thanks for the warning, @$$anonymous$$arsallima - I fixed this asha$$anonymous$$g error.

avatar image Marsallima · Nov 26, 2011 at 02:08 PM 0
Share

xDDD but how i make to turn SelectWeapon(1); to a name of object? i try SelectWeapon( Weapon$$anonymous$$4A1 (Is this name of the object) ); and not work :/ ( Sorry for my english xD )

avatar image syclamoth · Nov 26, 2011 at 02:38 PM 1
Share

That's how you use this function!

 SelectWeapon("Weapon$$anonymous$$4A1");

will select the object specifically named Weapon$$anonymous$$4A1.

Personally I don't think this is a very good way to do this, but it does what you asked for.

avatar image aldonaletto · Nov 26, 2011 at 05:16 PM 0
Share

As usual, @syclamoth is right (I can't count how many times I wrote this!): you must pass the weapon name as a String, with the enclosing quotes.

Show more comments

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

Weapon Shake 2 Answers

Handgun animation to play once when gun is shot in-game 1 Answer

FPS animations for different purposes? 1 Answer

Switch Weapon Script Not taking effect HELP!! 2 Answers

Aim Down Sights and Weapon Sway not working unless animations are not assigned 0 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