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

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