Bool is not returning a new bool value
So I wanted to have a timer that after a cetrain time turns a bool to false. My idea was that he bool would be called and in it, it would count down 0.5 seconds and then send back the bool.
However I do not get sent back the bool. It will call the bool once, the "Warning" gameobject will spawn and the bool will turn to "true", but it does not change back to false.
There has to be a logical error somehwere right? I guess but it's just not clicking for me. Unless there is something I don't know yet in how C# works with returning values.
Here's the code:
bool WarningSpawned = false;
public GameObject Warning;
void FixedUpdate()
{
Debug.Log("The warning state is : " + WarningSpawned);
if (WarningSpawned == false)
{
WarningSpawned = true;
SpawningWarning(WarningSpawned, Warning);
}
}
bool SpawningWarning(bool WarningSpawned, GameObject Warning)
{
float WarningCooldown = 0.5f;
CountdownNumber -= Time.deltaTime;
Instantiate(Warning, transform.position, Quaternion.identity);
if (WarningCooldown <= 0)
{
WarningSpawned = false;
}
return WarningSpawned;
}
Does anyone know what it could be? Is maybe Time.deltaTime not allowed to be anywhere but Start, Update and FixedUpdate? Thanks
Answer by Larry-Dietz · Dec 04, 2019 at 09:49 PM
You have WarningSpawned as a global variable, at the top.
You are passing it to SpawningWarning unnecessarily, causing that routine to use a private local copy of the variable, so your global variable is not being changed by the routine.
Also, that routine is only being called 1 time, when the bool is False, so your countdown is not working.
Here's the best way to fix this, in my opinion
bool WarningSpawned = false;
float WarningCooldown = 0.5f;
public GameObject Warning;
void FixedUpdate()
{
Debug.Log("The warning state is : " + WarningSpawned);
if (WarningSpawned == false)
{
WarningSpawned = true;
SpawningWarning(Warning);
}
else
{
WarningCooldown -= Time.deltaTime;
if (WarningCooldown <= 0)
{
WarningSpawned = false;
WarningCooldown=0.5f //Reset it for next time
}
}
}
void SpawningWarning(GameObject Warning)
{
Instantiate(Warning, transform.position, Quaternion.identity);
}
Hope this helps, -Larry
One thing to keep in $$anonymous$$d here, also, is that as soon as WarningSpawned changes back to false, then very next update is going to set it back to true, and start the cycle again.
If you are not wanting this to just cycle back and forth, you will probably want the initial change from false to true to occur in a trigger somewhere.
Yes this works just as I wanted. Thank you.
And yes. I should have mentioned above but I did want this to loop.
And to learn from this, does this means that time countdowns do not work inside whatever is called? Is it because when the bool was called, the times was still at 0.5 seconds, so above 0 thus it did not go into the if statement to turn the bool to *"true?"
Glad I could help.
Basically, yes, Your cooldown time was being set to 0.5 in the one and only time that function ran. It never ran again, so it never counted down. (However, since you set the timer to .05 at the top of that function, even if it did run again, it would just set it to .05 and still never hit 0) Only the update functions run over and over, unless you setup a function to loop like that with InvokeRepeating, or by calling it from within a loop (or from an update function)
So what the modified FixedUpdate is doing now, is if your bool is false, it sets it to true, and calls the SpawningWarning function.
On the next update cycle, your bool is true, so the else clause runs, reducing your cool down timer until it hits 0 or less. When it hits 0 or less, it resets your bool and sets your cool down timer back to the initial value for the next run.
Also, a couple of other things to point out.
In your SpawningWarning function, you are passing in the warning object. But this object is global, so would already be available to the function.
It could have been like this...
void SpawningWarning()
{
Instantiate(Warning, transform.position, Quaternion.identity);
}
with the call in FixedUpdate being ... SpawningWarning();
And while we are simplifying things, unless you are going to eventually make SpawningWarning do something other than the instantiate, I would just eli$$anonymous$$ate it entirely, and do the instantiate in the FixedUpdate like this...
bool WarningSpawned = false;
float WarningCooldown = 0.5f;
public GameObject Warning;
void FixedUpdate()
{
Debug.Log("The warning state is : " + WarningSpawned);
if (WarningSpawned == false)
{
WarningSpawned = true;
Instantiate(Warning, transform.position, Quaternion.identity);
}
else
{
WarningCooldown -= Time.deltaTime;
if (WarningCooldown <= 0)
{
WarningSpawned = false;
WarningCooldown=0.5f //Reset it for next time
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612215714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can I store a value computed in one method outside of that method? 2 Answers
Not all paths return a value 1 Answer
[C#] Not all code paths return a value 2 Answers
Returning Values from custom namespaces C# 1 Answer
How do I reference an incremented int with one button, that was incremented from another button? 1 Answer