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 Blink · Aug 20, 2012 at 10:16 AM · guicountdownammo

GUI countdown when held?

At the moment Ive got this script working that counts down the ammo by one when the LMB has been clicked once, this is for a semi automatic sort of gun. Know I'm trying to change it so it counts down when the LMB is being held, I have tried over and over but I keep getting tons of errors, can anyone help me out?

This is my semi auto script,

 var fullClip : int = 8;
 
 var bulletsLeft : int = 8;
 
 var reloadTime = 1.5;
 
 var myFont:Font;
 
 var someInt : int = 20;
 
 
 
 
 
 function Update () {
 
     if(Input.GetButtonDown("Fire1")){
 
         if(bulletsLeft > 0 ){
 
             bulletsLeft -- ;
 
             }
 
     }
 
 
 
     if(Input.GetButtonDown("r")){
 
         Reload();
 
 
 
     }
 
 }
 
 
 
 function Reload () {
 
 
 
   yield WaitForSeconds(reloadTime);
 
         bulletsLeft = fullClip ;
 
 
 
 }
 
     
 
 function OnGUI() { 
 
     GUI.Label (Rect (25, 20, 100, 20), ""+bulletsLeft);
 
     GUI.skin.label.font = myFont;
 
     GUI.skin.label.fontSize = someInt;
 
 }
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
Best Answer

Answer by Khada · Aug 20, 2012 at 10:49 AM

 Input.GetButtonDown("Fire1")

...will only return true once, when the key is first pressed.

 Input.GetButton("Fire1") 

...will return true so long as the key is held down.

If that's returning you errors, you need to post them up.

EDIT: Here is an example that is tested and works (and if it doesn't, your issue is elsewhere):

 public class BulletTest : MonoBehaviour
 {
         public int iClipSize = 10;
         public float fReloadTime = 2;
        
         private float fReloadCount = 0;
         private int iBullets;
         private bool bReloading = false;
        
         // Use this for initialization
         void Start ()
         {
                 iBullets = iClipSize;
         }
        
         // Update is called once per frame
         void Update ()
         {
                 if(Input.GetKey(KeyCode.A))
                 {
                         if(iBullets > 0)
                                 Debug.Log("Bullets :" + --iBullets);
                 }
                
                 if(Input.GetKey(KeyCode.R))
                 {                      
                         bReloading = true;
                 }
                
                 if(bReloading)
                         Reload();
         }
        
         void Reload()
         {
                 fReloadCount += Time.deltaTime;
                
                 if(fReloadCount > fReloadTime)
                 {
                         Debug.Log("Reloading");
                         //yield return null;//new WaitForSeconds(fReloadTime);
                         Debug.Log("Reloaded!");
                         iBullets = iClipSize;
                         bReloading = false;
                         fReloadCount = 0;
                 }
         }
 }
Comment
Add comment · Show 4 · 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 Blink · Aug 20, 2012 at 11:23 AM 0
Share

This doesn't seem to change anything, I don't get any errors either nor with the script I already put up.

avatar image Khada · Aug 20, 2012 at 12:14 PM 0
Share

Updated answer with a working example. Compare this against your own.

avatar image Khada · Aug 20, 2012 at 05:07 PM 0
Share

Did that do the trick? Don't forget to mark the question as answered if it did.

avatar image Khada · Aug 21, 2012 at 01:22 PM 0
Share

Ps. Questions can be marked as answered by clicking via the checkmark below the up/down vote buttons on any answer.

avatar image
0

Answer by Grug16 · Aug 20, 2012 at 11:54 AM

This question was hard to answer because you mentioned GUI, so I thought you meant a GUI.Button.

Instead of Input.GetButtonDown("Fire1"), you should use Input.GetButton("Fire1"). That will return true as long as the button is held, instead of just the frame it's pressed.

Hope that helps.

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 Khada · Aug 20, 2012 at 12:03 PM 0
Share

Why give the exact same answer that's already been given? =\

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

9 People are following this question.

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

Related Questions

Coin Counting Problem 0 Answers

Ammo GUI Help 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

gui help for ammo display 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