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 Destrark · Jul 17, 2016 at 08:34 PM · uicounterammo

Please Help figure out whats wrong with Ammo Counter.

Hi, trying to figure out why the ammo script I'm building isn't working. It's a C# script for Unity 5.3 and I'm trying to build a script that does a score counter of 5 attached to a UI.Text attached to the canvas. Final score counter in camera view should be Ammo : 5 with the 5 decreasing with every shot. AmmoGUItext is the name of the UI.Text component.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class AmmoCounter : MonoBehaviour {
 
     int Ammo = 5;
     public UIText AmmoGUItext;
 
     void Update()
 {
     if(Ammo != 0) 
     {
     if(Input.GetButtonUp("Fire1"))
         Ammo--;
        }
     }
     void On() {
         AmmoGUItext.text = "Ammo: "Ammo");
     } 
 }
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
0

Answer by Destrark · Jul 18, 2016 at 06:15 AM

Problem fixed: [code] using UnityEngine;

2.using System.Collections;

3.using UnityEngine.UI;

5.public class AmmoCounter : MonoBehaviour {

7.public int Ammo = 5;

8.public Text AmmoGUItext;

10.void Update()

11.{

  1. if(Ammo != 0 && Input.GetButtonUp("Fire1"))

  2. {

  3.    Ammo--;
    
    
    
  4. }

16.}

17.void OnGUI()

18.{

  1. AmmoGUItext.text ="Ammo: " + Ammo);

20.}

21.}

[/code]

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
avatar image
0

Answer by Srki94 · Jul 18, 2016 at 12:05 AM

Here's cleaned up version of code provided :

  int Ammo = 5;
 public Text AmmoGUItext;

 void Start()
 {
     AmmoGUItext.text = "Ammo : " + Ammo;
 }

 void Update()
 {
     if (Input.GetButtonUp("Fire1") && Ammo > 0)
     {
         Ammo--;
         AmmoGUItext.text = "Ammo : " + Ammo;
     }
 }

Update runs once per frame. We check if player has released button "Fire", and if that is true, we also check if Ammo value is greater than 0. Only when both conditions are true our If block of code runs.

So we can use && to chain multiple conditions inside If statement.

In your original statement you check if value of Ammo is not 0. But that is also true if player has -6 ammo.

Finally we reduce Ammo value by one and update text property of Text component (which you need to drag to public field in Inspector). We first set value to "Ammo :" and then we add (convert to string) actual value of the ammo counter.

Where did you find type "UIText" by the way ? Next time also include error that console reports back, when you try to run the game.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity UI not rendering ammo icon when build unity 2018.4.18 1 Answer

Gun Ammo Counter - Set frame of sprite sheet 2 Answers

Gun Script not working help please 2 Answers

problem with ammo count 1 Answer

When I switch weapons the UI ammo count does not change to match the current weapon 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