How to Subtract Time from Progress Bar Timer?

Hello,

I have a graphic progress bar which I declare here:

var barDisplay : float = 0;

It counts up from zero and if barDisplay is >=1 the game is over. That works fine.

I also have:

function subtractTimeFromBar () {
	
	//How do You Subtract Time from barDisplay here?
	Debug.Log ("addTimeToBar Called");

}

Which is called from a collision. This also works and is getting called as subtractTimeFromBar shows in the console.

When addTimeToBar is called, I would like to subtract anyNumberIsFine amount of time from the timer.

I’m not sure how to do this.

Any help is really appreciated.
Thanks in advance!

Just subtract off x from the barDisplay variable. It would probably be best to pass in x as a parameter to your subtractTimeFromBar function.

barDisplay = barDisplay - x;
if (barDisplay < 0) barDisplay = 0;

Or, since you know barDisplay will always range from 0 to 1, you can use the Mathf.Clamp01 function to remove the if

barDisplay = Mathf.Clamp01(barDisplay - x);

This wasn’t working because my timer was using Time.time. I had:

barDisplay = Time.time * .05

and should of used just counted up from 0.001