• 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 ivansc · Mar 19, 2014 at 08:48 PM · systemweaponswap

how to change the gun but keep the bullets

i already asked this but i didnt get any answer and i really need this to be working, so here it is a little more detailed, i have a weapon system where i change between them by pressing the numbers bar(on top of the keyboard) the weapons are instantiated, so everytime i a press a number, it instantiates a new clone of the prefab. how can a i make the guns keep the bullets when i change the current gun instead of get fully charged everytime? excuse my english

 for (int i = 0; 0 < guns.Length;  i ++) 
             {
 
             if(Input.GetKeyDown((i + 1) + "") || Input.GetKeyDown("[" + (i + 1) + "]"))
                 {
                 EquipGun(i);
                 break;
                 }
         }
 }
 
     void EquipGun(int i) {
         if (currentGun) {
             Destroy(currentGun.gameObject);
         }
 
         currentGun = Instantiate(guns[i],handHold.position,handHold.rotation) as Gun;
         currentGun.transform.parent = handHold;
         currentGun.gui = gui;
         animator.SetFloat("Weapon ID",currentGun.gunID);
     }
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 TykoX64 · Mar 19, 2014 at 08:55 PM

The simplest way would be for your player to store how many bullets are left for each gun and then update that property of the gun after you instantiate it.

If you want something a little more solid then you can have the play store references to each gun and only use one at a time. I.E. Have GameObject[] guns; and int activeGun; to access the current gun.

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 ivansc · Mar 21, 2014 at 04:59 PM 0
Share

i want only a primary and a secondary weapon, how can i do to store the total bullets and the bullets in mag, i mean, the primary and the secondary are in an array

30(bulletsIn$$anonymous$$ag)/90(TotalBullets)

GameObject Gun[] guns;

how can i access to one of the two guns to store the data?...

sorry if i explained this wrong.

avatar image TykoX64 · Mar 21, 2014 at 05:30 PM 0
Share

What you can do is create a script for your gun that will handle everything about it like firing and how many bullets it can hold vs how many are currently in the mag.

 public class Gun : $$anonymous$$onoBehaviour
 {
    public int bulletsIn$$anonymous$$ag;
    int maxBulletsIn$$anonymous$$ag;
 
    //Add the rest of the code you want the gun itself to handle like firing.   
 }

You need to add the two guns to your player in your guns[] array. Then you can keep a variable (if it's 2 guns then a bool will work, for more guns you may need an int) to track which gun you're using.

 public class Player : $$anonymous$$onoDevelop
 {
 // make this public so you can prefab your two guns and add them in the inspector.
    public GameObject[] guns;
 
    bool gunNumber1 = true;
 
    void Update ()
    {
       if (Input.GetButtonDown("switchWeapon"))
       {
          //this is how we switch between the two guns.
          gunNumber1 = !gunNumber1;
       }
    }
 
    void AccessCurrentGun ()
    {
       if (gunNumber1)
       {
          guns[0].getComponent<Gun>().bulletsIn$$anonymous$$ag;
       }
       else
       {
          guns[1].getComponent<Gun>().bulletsIn$$anonymous$$ag;
       }
 }

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to do Switching Weapon System 1 Answer

Weapon swap help 1 Answer

i need help with my weapon stats 1 Answer

Best way to send variables/data to other scripts? 1 Answer

Multiple Weapon System Implementation. 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