How to make an energy bar drain & fill based on holding a button

I am using a circular energy bar to display energy. Currently I have an alphamap that is filling & draining the texture for the desired results. My issue is that I cannot get it to fill back up when I let go from where it left off. Here is the incomplete code as I have it now:

public var energyTick = 0.1;

public var totalTick = 0.0;

function Update () {

if (Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift))


	totalTick = Time.deltaTime + totalTick  + energyTick;
		if (totalTick > 249){
		totalTick = 250;
		 var Energytotal = totalTick;
	Energytotal = Time.deltaTime - 1 - energyTick;

renderer.material.SetFloat(“_Cutoff”, Energytotal);


I cant get the energy total or “current energy” to leave the first if statement so I can script it to refill. Essentially at full it starts as 0, at empty it is 250. I am trying to have it detect that shift is pressed then start draining it from 0 to 250, then when it hits 250 OR when shift is let go it then begins to fill back up. In my controller i have the shift key as a way to go from trot speed to run speed. I am essentially trying to limit its usage.

Long term I plan to have it detect character state, not shift key, and if character state is running then start the drain. When drain maxes out then character state becomes trotting. If character state is not running then begin the fill till it caps out from current to 0.

Any suggestions? Im new to C# but I would prefer to stick with it.

I am essentially adapting this HP bar to work as an energy bar but with different textures:

Try using

Energytotal -= Time.deltaTime - 1 - energyTick;

add the minus sign before equivalent.