- Home /
Bool not being re-triggered in FixedUpdate or Update
Hi, I've been pounding at this code all day, can't figure it out. I have two turrets that once fired, activate a cooldown state of about 44 seconds, along with shrinking a status bar using time.time (using time.deltatime only shrinks it by 1.0 then stops). I can get the status bar to shrink, and reset the bool, but for some reason the bool won't reset after firing again.
Here is the code for one of the turrets (the one for the second is identical, only 1 replaced with 2 for the second one)
if (activeCD1)
{
Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.red;
Turret1Status.sizeDelta = new Vector2(tur1width - 1.0f * Time.time, 6);
if (Turret1Status.sizeDelta.x <= 0)
{
activeCD1 = false;
Turret2Status.sizeDelta = new Vector3(44, 6);
}
}
else
{
Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.green;
Turret1Status.sizeDelta = new Vector3(44, 6);
}
There is some bizarre de-synch with tur1width and Turret1Status.sizeDelta.x. It properly goes down, and the activeCD1 gets set to false and status bar is reset to normal size. But when the function gets called again, turret1status.sizedelta.x is still 0. I tried everything to set it back to 44, but nothing works. Is there something I'm missing, or am I doing this wrong?
Here is the function that calls activeCD1 to be true incase there's something I missed:
public void Turret1Off()
{
activeCD1 = true;
Turret1Active = false;
hp1.SendMessage("TurnOff");
emit = false;
beamtake = 0;
runcycle = 0;
}
At least you have this line in there
Turret2Status.sizeDelta = new Vector3(44, 6);
When all the other lines talk about Turret1.
Looks like you are using a lot of static variables and different scripts for turrets that should probably work exactly the same. Thats a bit hacky way of going about things and that's exactly what makes the code prone to this kind of copy/paste mistakes (if this is the problem. $$anonymous$$aybe I don't understand the whole picture after seeing these bits of code)
If I understand correctly Turret1Status.sizeDelta.x reaches 0 so you switch a bool. When you switch the bool back you expect Turret1Status.sizeDelta.x to be greater than 0. However none of the shown code actually changes the value of tur1width and Time.time gets bigger over time so why would (tur1width - 1.0f * Time.time)
go back over 0?
also depending on your canvas parameters and your layouts you may not be able to override the size of your UI element. $$anonymous$$ake sure you can manually change it through the inspector at the right time, if not then it is a layout problem.
@Nerevar has a good point also in the sense that it's a bit dangerous to use a property of an UI element to store the value of your cool down duration. You can't be sure what happens inside the property when you set it or get it.
@Nose$$anonymous$$ills: Caught it after the post, but fixed it. Forgot to post it about it. $$anonymous$$y variables are all public, not static. And I type my code by hand more than half the time. I know the horrors of copy/pasting code can create. I also don't have the UI actually storing the cooldown, it just shrinks like a status bar. I just have the bool to indicate when its cooling down and the turrets fire when its false.
@$$anonymous$$acccabbe: I saw a method to use Time.Time, I figured this was the cause, but wasn't sure of a way around it. $$anonymous$$aybe a coroutine or something?
@Nerevar: What setting would allow me to change it through script? Still not totally familiar with the new UI stuff yet.
Answer by GargoylExtreme · Aug 04, 2015 at 10:39 AM
Alright, so I ended up solving it myself. Coroutines were the answer. Both turrets now stop with the cool down timer running as expected, and restart the cooldown once repeated as well. Here is the code I used:
if (activeCD1)
{
Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.red;
Turret1Status.sizeDelta = new Vector2(tur1width,6);
if (tur1width < 1.0f)
{
Debug.Log("Recharged");
StopCoroutine(CoolDown1(44,0,0));
activeCD1 = false;
}
}
else
{
Turret1Status.GetComponent<UnityEngine.UI.Image>().color = Color.green;
Turret1Status.sizeDelta = new Vector3(44, 6);
tur1width = 44;
}
public void Turret1Off()
{
activeCD1 = true;
Turret1Active = false;
StartCoroutine(CoolDown1(44, 0, 10.0f));
}
private IEnumerator CoolDown1(float start, float finish,
float time)
{
if (tur1width <= 0)
yield return null;
float elapsedTime = 0;
tur1width = start;
while (elapsedTime < time)
{
tur1width = Mathf.Lerp(start, finish, (elapsedTime
/ time));
elapsedTime += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611234137im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer
Interpolation with jumping - Acting Weird. 1 Answer
FixedUpdate performance doubts 2 Answers
Placing Input.GetkeyDown() inside FixedUpdate() slows down the rate I can fire at 1 Answer
activating multiple voids with the same name trough one line 1 Answer