x


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;

}
more ▼

asked Aug 20 '12 at 10:16 AM

Blink gravatar image

Blink
106 10 19 28

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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;
                }
        }
}
more ▼

answered Aug 20 '12 at 10:49 AM

Khada gravatar image

Khada
2.2k 2 9

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

Aug 20 '12 at 11:23 AM Blink

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

Aug 20 '12 at 12:14 PM Khada

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

Aug 20 '12 at 05:07 PM Khada

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

Aug 21 '12 at 01:22 PM Khada
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 20 '12 at 11:54 AM

Grug16 gravatar image

Grug16
26 1 2 6

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

Aug 20 '12 at 12:03 PM Khada
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x3689
x66
x62

asked: Aug 20 '12 at 10:16 AM

Seen: 278 times

Last Updated: Aug 21 '12 at 01:22 PM