Incorrectly implemented coroutine

Hello! Such a problem arose: I want to realize energy in my game, but it doesn’t work. The script through “Start” does not work. Tried through “Update”, but then the coroutine calls each frame and the value of “_energy” decreases too quickly. Thanks in advance to everyone who helps!

The code inside the coroutine is only called once. I assume you want to repeatedly decrease the energy every second so put that inside of a while true loop or some condition. You are right, it should be only called once in start.

IEnumerator _minusEnergy() {
{ 
         while(true) {
                 _energy -= 3;
                yield return new WaitForSeconds(1f);
         }
}

Also Start is only called once before update so your code if _start == true condition in Start is likely to not be called. I assume that you want to repeatedly turn the energy coroutine on and off so you’d need to check if it is already started. Here’s some pseudocode on that.

if (input shift && energy >= 3 && !_start) {
        start = true;
        StartCoroutine(minusEnergy());
}
if (!input shift) {
        start = false;
        StopCoroutine(minusEnergy());
}

Hope this helps.