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 bdr · Feb 15, 2011 at 12:53 PM · gunweaponmanagerscript-ready-to-use

gun manager how to

does anyone know how to make a gun manager script in javascript? if u pick up a gun it adds it to an inventory or something a little like that.i guess u have to add a new input but i dont know where to go from there. i need the gun to be part of my character when he picks it up(i have my own character)

thank you

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

2 Replies

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

Answer by Jason_DB · Feb 15, 2011 at 08:09 PM

Basically you just need an array to store the weapons in, from which you can select them. I do it this way (although it's not the prettiest, it allows 1-10 weapons). Basically, if you hit a number key 1-0 it selects a weapon from the array. All you would need to do is have your guns on the character but turned off (invisible/not able to fire) and have functions on the gun to turn them on or off, then if you put it in the weapons array it will be selectable.

var weapons : GameObject[]; var selectedWeapon : int; function Update () { if (Input.GetKeyDown("1") && weapons.length >= 1) { SelectWeapon(0); selectedWeapon = 0; } else if (Input.GetKeyDown("2") && weapons.length >= 2) { SelectWeapon(1); selectedWeapon = 1; } else if (Input.GetKeyDown("3") && weapons.length >= 3) { SelectWeapon(2); selectedWeapon = 2; } else if (Input.GetKeyDown("4") && weapons.length >= 4) { SelectWeapon(3); selectedWeapon = 3; } else if (Input.GetKeyDown("5") && weapons.length >= 5) { SelectWeapon(4); selectedWeapon = 4; } else if (Input.GetKeyDown("6") && weapons.length >= 6) { SelectWeapon(5); selectedWeapon = 5; } else if (Input.GetKeyDown("7") && weapons.length >= 7) { SelectWeapon(6); selectedWeapon = 6; } else if (Input.GetKeyDown("8") && weapons.length >= 8) { SelectWeapon(7); selectedWeapon = 7; } else if (Input.GetKeyDown("9") && weapons.length >= 9) { SelectWeapon(8); selectedWeapon = 8; } else if (Input.GetKeyDown("0") && weapons.length >= 10) { SelectWeapon(9); selectedWeapon = 9; } }

function SelectWeapon (index : int) { for (var i : int=0 ;i<weapons.length; i++) { if (i != index){ weapons[i].gameObject.BroadcastMessage("deselectWeapon"); //the function deselectWeapon is in my gun script, and turns them off.

 }else{
     weapons[i].gameObject.BroadcastMessage("selectWeapon");
     //the function selectWeapon is in my gun script, and turns them on.

 }

}

}

Comment
Add comment · Show 4 · 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 bdr · Feb 16, 2011 at 09:52 AM 0
Share

whoa thanks a bunch man (or girl) expect a lot more questions from me in the future :)

avatar image Jason_DB · Feb 16, 2011 at 07:55 PM 0
Share

No problem! I made a package for making FPS games (handling weapons and such), so I can easily just pull stuff from my code which I know works.

avatar image Donilias · Jan 13, 2012 at 01:01 AM 0
Share

Sorry but i am to late with this reaction. I'm using this script. But when i press 1 for the weapon, the game gives me an error. This is the error: Broadcast$$anonymous$$essage selectWeapon has no receiver! UnityEngine.GameObject:Broadcast$$anonymous$$essage(String) Error in line 45

how can i turn the gun on and off?

avatar image syclamoth · Jan 13, 2012 at 01:06 AM 0
Share

Well, obviously this is because the message doesn't have a receiver? The error message tells all! Do you know how Send$$anonymous$$essage works? It calls a function on that object which has the name specified by the string paramater. In this case, "selectWeapon". You need to have a script on the weapon in question which includes the function "selectWeapon()". You can implement it however you choose.

avatar image
0

Answer by Kona · Feb 15, 2011 at 05:03 PM

I use C# myself so can't give you any example code but what I would try in this case is to create a list in your characters class where you store all weapons you have picked up.

Then set up a way to swap between the weapons in that list when pressing a button, and for the weapon currently equiped, put it in a temp cache and remove it from the inventory. And set up another button for droping a weapon, for that simply remove the equiped weapon from the cache (since it's already removed from the inventory as that's done when equiping it).

oh and before equiping a new weapon ofcourse you'ld add the currently equiped one back to inventory so we don't have two weapons equiped or accidentally destroy the equiped one completly.

:) And I suppose when pressing the button to swap weapons I'ld probably call another method aswell that would instantiate the prefab/model of the currently equiped weapon at the point where the weapon should be positioned, and then make it a parent of the player character / camera

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 bdr · Feb 16, 2011 at 09:53 AM 0
Share

whoa thanks a bunch man (or girl) expect a lot more questions from me in the future :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Switch Weapon Script Not taking effect HELP!! 2 Answers

Total Bullets to weapon reload? Help 1 Answer

Bullet Penetration 1 Answer

weapon manager 1 Answer

Select Object ( Weapon ) Quick! Help! xD 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