- Home /
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
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).
if(ammobar.GetComponent(UI.Slider).value <= 10)
{
fire.GetComponent(UI.Button).interactable = false;
}
else if(ammobar.GetComponent(UI.Slider).value >= 75)
{
fire.GetComponent(UI.Button).interactable = true;
}
i would try something like this.
Answer by Hellium · Jul 15, 2015 at 09:59 PM
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.
Answer by CaKeMeaT · Jul 15, 2015 at 09:55 PM
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
Thanks. Jeez, simple. I failed to look at this carefully. Thanks for your time.
Your answer
![](https://koobas.hobune.stream/wayback/20220611231503im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
increase/decrease the value of slider a little slowly 1 Answer
Fill Slider slowly to a given value. 2 Answers
Changing Volume with Slider - not working with Unity2017.3 anymore? 1 Answer
4.6 UI "Slider" jumps on handle press? 2 Answers
Why is UI Slider not working while Animator is enabled? 1 Answer