Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bryangs · Nov 21, 2016 at 07:21 AM · clickpointsonreduce

Problem with reducing points

I am working in a C# script that will reduce 1 to my weapon's ammo everytime the player press the left mouse button. I got no errors, so i think everything is fine, but when i test the game it only reduces one time, not everytime. So if i say that my ammo is 5, when i shoot it reduces to 4 and then it doesn't reduce the ammo anymore.

That's the script i am using

  public GameObject txt;
     private int ammo;
     public int bullets;
 
     void Start()
     {
         ammo = bullets;
         UpdateAmmo();
     }
 
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             ammo = bullets - 1;
             UpdateAmmo();
         }
     }
 
     void UpdateAmmo()
     {
         txt.GetComponent<UnityEngine.UI.Text>().text = ammo.ToString();
     }
 }
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 Dibbie · Nov 21, 2016 at 08:35 AM

Because your not actually reducing your ammo... "bullets" seems to be what is your actual ammo count, and all your saying is, "whatever the number is for ammo... Show that, but like, just show 1 number less", your not affecting the actual value "bullet" is. So what youd want to do is change ammo = bullets - 1; to something more like bullets -= 1; ammo = bullets; that way your actually updating bullets.

You will also run into the problem that, once your bullets hit 0... It will start to go into the negative numbers, if you DONT want that, then contain that idea into a if-statement, something like: if(bullets > 0){bullets -= 1; ammo = bullets;

Hope that helps.

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 Bryangs · Nov 21, 2016 at 02:17 PM 0
Share

$$anonymous$$y problem is that i want to use a single script for all my weapons, so i created bullet because then i can say what is the maximum bullets each weapon can have, i don't know if this is he best way, probaly not xD

avatar image Dibbie Bryangs · Nov 21, 2016 at 09:04 PM 0
Share

If you want to do that, you may want to look into creating an array of structs maybe, if you want to store information about multiple guns in one script - personally think doing that is a bad idea, but it IS do-able. (all untested code)

 public AllWeapons[] weapons = 5; //youd change this number to however many weapons you have in your game
 public AllWeapons currentWeapon;
 
 [System.Serializable]
 public struct AllWeapons {
 public int maxAmmo;
 public int currentAmmo;
 public float fireRate;
 public string weaponName;
 // and so on...
 };
 
 //in your Start
 void Start(){
 currentWeapon = weapons[0]; //your first weapon
 //from the inspector, you can set all the max ammos and stuff for each weapon
 }
 
 //in your Update, similar to what you have now
 
 void Update(){
 if(Input.GetButtonDown("Fire1")){
 currentWeapon.currentAmmo -= 1;
 UpdateAmmo();
 }
 }

Again... Id contest against doing this kind of system, just because I think its not needed, and it would be better to create a script for each weapon independently, then just reference the ammo on that script, rather then messing with Serializing, and arrays of structs and all that, but like I said, its still possible to do it the way you want.

avatar image Bryangs Dibbie · Nov 22, 2016 at 07:53 PM 0
Share

I fixed it. I realised that making a separate script just for reloading was a really bad idea, so i just copied the lines to my shooting script. I have only a problem now.

The if(bullets > 0){bullets -= 1; ammo = bullets; doesn't work. Unity doesn't recognize and bugs with the >. Do you know a way around? Don't let the player shoot indefinetely with negative bullets would be a great thing.

Show more comments
avatar image OusedGames · Nov 22, 2016 at 01:42 AM 0
Share
 if (Input.GetButtonDown("Fire1"))
          {
              ammo = ""ammo"" - 1;
              UpdateAmmo();
          }
avatar image Bryangs · Nov 23, 2016 at 05:31 PM 0
Share

You know, it doesn't work at all, but never$$anonymous$$d it's fine, i will fix it eventually. Anyway, at least now my ammo is updated accordingly, thanks!

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

apply click event in test 0 Answers

particle emitter on/off using the left mouse button 1 Answer

2D: How to Move On Click with Creatable (On Click) Waypoints 1 Answer

Can you determine the speed of falling? 3 Answers

How to make left clicking start an animation? 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