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 Padges · Sep 15, 2010 at 10:47 PM · fpschangeweaponindex

How to make a weapon index?

I am building off of the FPS tutorial script for changing weapons, such as "blah blah blah, change SelectWeapon(2) if 2 is pressed and all that. Then I have the SelectWeapon index code from the FPS tutorial also, but what I did is edit the code to include 5 weapons, not just 2. But the thing is, i dont know how to assign the index variable of the first weapon to SelectWeapon(1) or whatever.

Im using the exact same code from the FPS tutorial, I just dont know to how to set the weapons I have to the index values accordingly. :)

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 · Sep 16, 2010 at 03:00 AM

Hey Nick, welcome to UnityAnswers.

The SelectWeapon() function uses the GetChild() function to find the 'children' objects of the object that the SelectWeapon script is attached to. I've added comments to the script below to explain.

function SelectWeapon (index : int) 
{
   for (var i=0;i<transform.childCount;i++)  // loop through all the 'children'
   {
      // if the desired weapon value is the same as current child index value
     if (i == index)
     {
         // Activate the selected weapon
         transform.GetChild(i).gameObject.SetActiveRecursively(true);
     }
     else // otherwise
     {
         // Deactivate all other weapons
         transform.GetChild(i).gameObject.SetActiveRecursively(false);
     }
   }
}

As a result, you can't explicitly 'assign the index variable of the first weapon' - rather, you set the weapon to a child of the GameObject that this script is attached to. I'd assume the GetChild() function sorts by name, so you could just name the weapons "0_Fists", "1_Pistol", etc.

Duck's answer to a similar question on 'Weapon Switching' explains how to extend the if/else in the Update() loop, so pressing '3' will select the 3rd weapon, and so on.

Hope this helps!

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 user-4565 (google) · Jan 11, 2011 at 05:53 PM 0
Share

So $$anonymous$$arowi, Nick again, /i changed all the child objects, which are the weapons,to be named 1_$$anonymous$$elee, 2_Deagle, etc. but the issue is still arising.Any other ideas of how the index will sort?

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

FPS game: weapons 2 Answers

unlocking weapon (fps) 1 Answer

The Variable has not been assigned. 1 Answer

FireWeapon.cs not working 2 Answers

Player Animation change when pickup Weapon 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