How to get my players weapon to not work when its energy bar runs out

Hey there, so i have had no unity experience up until now and im having to create a 2d game for my level design class and i created a mechanism that has the player using a flashlight to defend himself from monsters and it uses an energy bar that has a cool down time and the longer the player holds a certain key down, the bar slowly goes down over time unless the player stops holding down the key. My issue is that the flashlight stays on even if the bar runs out and the player is holding the key down and i want it so that even if the player keeps holding down the key, the flashlight does not work as long as the bar is empty, so the player will have to allow the energy to regen before use. If anyone has an idea how to help, i would really appreciate it. attached is my flashlight script and the script for the energy bar.

![using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flashlight : MonoBehaviour
{
    [SerializeField] GameObject FlashlightLight;
    
    public bool FlashlightActive = false;
    public PlayerHealth ph;


    void Start()
    {
        FlashlightLight.gameObject.SetActive(false);
    }

    // Update is called once per frame
    private void Update()
    {
        if (FlashlightActive)
            FlashlightBar.instance.UseEnergy(15 * Time.deltaTime);
        if (ph.currentHealth > 0 && Input.GetKeyDown(KeyCode.E))
        {
            FlashlightLight.gameObject.SetActive(true);
            FlashlightActive = true;
        }

        if (Input.GetKeyUp(KeyCode.E))
        {
            FlashlightLight.gameObject.SetActive(false);
            FlashlightActive = false;
        }
        if (ph.currentHealth > 0 && Input.GetKeyDown(KeyCode.E))
        {

        }
        if (ph.currentHealth < 1)
        {
            FlashlightLight.gameObject.SetActive(false);
            FlashlightActive = false;
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Enemy")
        {
            var healthComponent = collision.GetComponent<DemonBatHealth>();
            if (healthComponent != null)
            {
                healthComponent.TakeDamage(1);
            }
        }
  
        if (collision.tag == "Enemy")
        {
            var healthComponent = collision.GetComponent<GhostEnemyHealth>();
            if (healthComponent != null)
            {
                healthComponent.TakeDamage(1);
            }
        }
    }
}][1]

Maybe this will work

//FlashlightBar
float timer = 0.0f;
public void UseEnergy(float amount)
{
    //use up energy
    if(currentEnergy > 0)
    {
        currentEnergy -= amount;
        if(currentEnergy < 0) currentEnergy = 0; //limit
        timer = 0.0f; //reset timer
    }
    flashlightBar.value = currentEnergy;
}
public void RegenEnergy()
{
    //regenerate energy at rate maxEnergy/10.0 per second
    timer += Time.deltaTime;
    if(timer > 2.0f) //begin regenerate after 2 sec
    {
        currentEnergy += (maxEnergy / 10.0f) * Time.deltaTime;
        if(currentEnergy >= maxEnergy) currentEnergy = maxEnergy; //limit
    }
    flashlightBar.value = currentEnergy;
}

//Flashlight
private void Update()
{
    if (FlashlightActive)
        FlashlightBar.instance.UseEnergy(15 * Time.deltaTime);
    FlashlightBar.instance.RegenEnergy();
    FlashlightActive = Input.GetKey(KeyCode.E);
    if (ph.currentHealth <= 0 || FlashlightBar.instance.flashlightBar.value <= 0)
        FlashlightActive = false;
    FlashlightLight.gameObject.SetActive(FlashlightActive);
}

I think it is easier to do things like this from Update() and not using coroutines.
However I think your current code could work if you just add a check for flashlightBar.value
and disable the FlashlightLight.gameObject if 0.

Hello @Rosebud098 this is a good question since it’s logic is required in almost every game.
At first it is good for you to have the flashilght energy logic inside of the actual Flashlight and not on the UI logic. I would do the logic entirely different, but for simplicity sake:

You could put on the UI script only the logic to change the slider (or other forms of UI) to represent the energy value.

then you could do inside Update:

    if (isPlayerAlive == false) return; //exit the method, this way you can't do weird stuff if dead
    if (Input.GetKeyDown(KeyCode.F)  &&  flashlightEnergy > 0f)
    {
        FireTheFlashlight();
    }
    else
    {
        HoldFireFlashlight();
    }
    
    //outside the Update you could have:
    private void FireTheFlashlight()
    {
        isFlashlightActive= true;
        //logic to fire the flashlight
    }
    private void HoldFireFlashlight()
    {
        if (isFlashlightActive)
        {
             isFlashlightActive= false;
            //logic to stop firing
        }
    }

I changed the variable FlashlightActive to isFlashlightActive to follow the variables convention more accurately.
Actually there are many ways to do this, here I present one of the most simple yet efficient way of doing it.
You could use a Flashlight.instance on your UI to get the energy value, or, you can at the end of your Flashlight Update send the energy through a method let’s say UpdateEnergyBar(float energyLevel) << Note that this one has to be created inside the UI logic and be called inside the Flashlight logic.

Hope it helps you