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 /
  • Help Room /
avatar image
0
Question by Bryangs · Nov 27, 2016 at 08:26 AM · limitammo0dontunder

Limit ammo

I am trying to make a single script in my game that covers all the combat system, and it is working as expected, but i am having a problem with the ammo part on it. I have a text and a public int named Ammo. The int holds the ammount of bullets that i have. Everytime i shoot, i will have one less bullet and so it goes. The problem is, when Ammo reaches 0 i can still shoot, and it goes indefinetely with negative numbers, such as -1,-2,-3... How would i limit my ammo so when i have 0 bullets the number will NOT change anymore, or just stay at 0?

I was trying to limit Ammo by using this, but it doesn't work:

   if (ammo == 0) //If ammo is equal to zero, Then | I also tried (ammo == curammo) curammo being a int with value 0.
         {
             ammo = 0; //Then, ammo is equal to zero and it stays there. | As i said before, using curammo, it would be like ammo = curammo then it would always stay at 0.
             UpdateAmmo(); //This updates my UpdateAmmo function.
         }

This is my UpdateAmmo function, it make my text be equivalent to the ammo int so i can know how many bullets i have.

  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

2 Replies

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

Answer by dacarrera · Nov 27, 2016 at 09:04 AM

I'm not sure what the rest of your code is, so you'll just have to amend this as needed.

Essentially you can just add an if() statement (as you were doing) but you should first check if ammo > 0 then run your function.

 if(ammo > 0){
     // Run whatever functions you want
     // when you have ammo
 }
 else{
     // everything else
     // AKA you don't have ammo (ammo <= 0)
     // also make sure ammo.text is set to 0
 }

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 Bryangs · Nov 27, 2016 at 03:47 PM 0
Share

Thanks, it worked perfectly as i wanted! =)

avatar image
0

Answer by ma22be61 · Nov 27, 2016 at 10:31 AM

First off , are you sure your ammo quantity is actually connected to the number of bullets or the shooting function? Maybe it's only "connected" to the textui and not the gun?

 void Update ()
 {
 if (Input.GetKeyDown(KeyCode.Mouse1) && ammo >=1)
 {
 ammo = ammo -1;
 UpdateAmmo();
 }
 else if (Input.GetKeyDown(KeyCode.Mouse1) && ammo ==0)
 {
 //STOP SPAWNING BULLETS
 }
 }

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can i limit position between 2 object ?? 1 Answer

Problem with my Weapon Recharge script 0 Answers

Don't stop music on Android (Override Ipod Music) 0 Answers

Moving to certain direction 1 Answer

Limit camera rotation on Y Axis when using a joystick 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