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 Nourero · Jul 14, 2013 at 10:54 PM · gun movement

Adding Ammo to make gun loose/add ammo as used/picked up

I have a gun that has many different features of shooting but I can not seem to find out how and where to fix so I can see how much ammo and bullets I have left. I need a simple GUIText and nothing else but from what I have tried I just could not figure out where and how. I hope any of you could help me. Thanks PS. This is not my personal script.

This is a part of the script.

void MachineGun_Fire(){

     if (bulletsLeft <= 0)
     {
         StartCoroutine("reload");
         
         return;
     }
     // If there is more than one bullet between the last and this frame
     // Reset the nextFireTime
     if (Time.time - fireRate > nextFireTime)
         nextFireTime = Time.time - Time.deltaTime;

     // Keep firing until we used up the fire time
     while (nextFireTime < Time.time)
     {
         switch (typeOfBullet)
         {
             case BulletType.Physical:
                 StartCoroutine("FireOneShot");  // fire a physical bullet
                 break;
             case BulletType.Raycast:
                 StartCoroutine("FireOneRay");  // fire a raycast.... change to FireOneRay
                 break;
             default:
                 Debug.Log("error in bullet type");
                 break;
         }
         shotsFired++;
         bulletsLeft--;
         nextFireTime += fireRate;
         EjectShell();
         Kick();
     }
     
 }
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 l5p4ngl312 · Jul 14, 2013 at 11:27 PM

Just make a new guiText (a gameObject with a guiText component). Make a variable in your script: public GameObject guiTextObject and put your guiText object in that slot in the editor. Then in your script just use guiTextObject.guiText.text = bulletsLeft.ToString();

 public GameObject guiTextObject;
 
 void MachineGun_Fire(){
 
     if (bulletsLeft <= 0)
     {
         StartCoroutine("reload");
  
         return;
     }
     // If there is more than one bullet between the last and this frame
     // Reset the nextFireTime
     if (Time.time - fireRate > nextFireTime)
         nextFireTime = Time.time - Time.deltaTime;
  
     // Keep firing until we used up the fire time
     while (nextFireTime < Time.time)
     {
         switch (typeOfBullet)
         {
             case BulletType.Physical:
                 StartCoroutine("FireOneShot");  // fire a physical bullet
                 break;
             case BulletType.Raycast:
                 StartCoroutine("FireOneRay");  // fire a raycast.... change to FireOneRay
                 break;
             default:
                 Debug.Log("error in bullet type");
                 break;
         }
         shotsFired++;
         bulletsLeft--;
         guiTexObject.guiText.text = bulletsLeft.ToString();
         nextFireTime += fireRate;
         EjectShell();
         Kick();
     }
  
 }

Now all you have to do is make a new guiText (GameObject -> Create Other -> GuiText), position it how you want, and then select the object that contains your gun scripIn the editor drag the new guiText object to where it says guiTextObject in the inspector. If you don't know how to do that you probably need to watch some more basic Unity tutorials. Here's more info on guiTexts: http://docs.unity3d.com/Documentation/Components/class-GuiText.html

Comment
Add comment · Show 3 · 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 Nourero · Jul 15, 2013 at 01:12 AM 0
Share

Could you give a small example maybe? I'm still learning in school and the whole system is not very good in it's $$anonymous$$ching. Appreciate your quick answer.

avatar image l5p4ngl312 · Jul 15, 2013 at 11:45 AM 0
Share

Added to answer

avatar image Nourero · Jul 15, 2013 at 02:59 PM 0
Share

Works perfect thanks a lot :D

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

16 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

Related Questions

How to sprint while holding a weapon? 0 Answers

Delayed Rotation 0 Answers

How to make a gun work with camera? 1 Answer

Gun is not rotating while player is in a animation 0 Answers

Simulate 'Child' Effect by Script 2 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