Reset float not working

I’m trying to set up a charge shot for a weapon in my game, the player holds down the fire button and releases once the charge has built. However once the charge has built and the shot fired the float I have set up to count to 100 wont reset back to 0. This is my script:

void Update ()

{
        if (Input.GetButtonDown("Fire5"))
        {
            charging++;
        }
       
        if (Input.GetButtonUp("Fire5"))

        {
            if ((Time.time > nextFire) && charging >= 100)
            {
                nextFire = Time.time + fireRate;
                Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
                audio.PlayOneShot(impact, Volume);
                charging = 0;
             
            }
            else
            {
                charging = 0;
                return;
            }
        }

}

I thought putting ‘charging = 0’ would reset the counter but it doesn’t. The counter resets as long as the button is released before it reaches 100.

Well, it looks right to me, so I’m not sure what’s up with that :slight_smile:

The only thing that I see odd with that script is the return; in the bottom of statement. Not sure why you have that there when it’s in Update , and also the very end of your code (nothing will get executed after that in Update…), and it’s not returning a value. So unless it’s doing something fancy I am unfamiliar with, I’d remove it. Couldn’t see it breaking anything… it shouldn’t… But I guess you never know. I assume that’s like saying return null or void though so probably fine, just never seen someone do that in a void function or trigger.

Are you setting this variable somewhere else too, perhaps?

I have accidentally found the problem and fixed it. It was related to the audio file funnily enough. Because I had forgot to put ‘audio = GetComponent();’ in the Start() this was stopping the float reset from working. Weird, but it works so i’m happy. Also I have removed Return because as you pointed out it was pointless, thanks :slight_smile:

Ya, if it threw an exception, it would “jump?” out of that area of code’s execution ; therefore, not making it far enough :slight_smile:
Glad you got it fixed.