Shooting Cooldown Bar

Hi everyone,

I need some help on my shooting-system. My turret has unlimited ammo, but to prevent constant shooting I want to have a cooldown-effect that stops shooting, to simulate an overheated-effect of the turret. For visualization I have added a slider (Values 0-100)
In case one has the turret really overheated (Slider-value <= 10) one can not shoot anymore until the value reaches 75. I have written the code but is not working.

if(ammobar.GetComponent(UI.Slider).value <= 100){
	ammobar.GetComponent(UI.Slider).value += 0.25;
}
if(ammobar.GetComponent(UI.Slider).value <= 10){
	fire.GetComponent(UI.Button).interactable = false;
    if(ammobar.GetComponent(UI.Slider).value >= 75){
	    fire.GetComponent(UI.Button).interactable = true;
    }
}

The problem is that the button is not interactable at the given value (75).

The problem comes from the following line :

if(ammobar.GetComponent(UI.Slider).value <= 10){
     fire.GetComponent(UI.Button).interactable = false;
     if(ammobar.GetComponent(UI.Slider).value >= 75){
         fire.GetComponent(UI.Button).interactable = true;
     }
 }

The second if will never be reached since you check if the value is less or equal than 10 before and if the expression is true, you check if the value is greater or equel to 75. The value can’t be <= 10 and >= 75 !

Just take the second if outside the first one like @OctoMan suggested.

By the way, getting the component like you do is a very bad idea. Prefere getting the component once in the start and use it in your function.

the problem with greater-or-equal-than opertaions is that they can be pass as true even when you expect the later conditions to trigger instead.

ammobar.GetComponent(UI.Slider).value <= 100) will almost always be true it seems.

and your second if statement only allows you to check for >= 75 when you’ve already checked if your value is <= 10.

So for example, your ‘Value’ is 40, first IF is negative, so your nested if doesn’t check