- Home /
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!
It would help if you could tells us how you're calculating the time from the progress bar and how your gameobject's components are organized.
In my Update function I have:
barDisplay = Time.time * .05;
To count the timer up.
I call subtractTimeFromBar() from another script:
function OnTriggerEnter (collision : Collider) {
//doStuff
}
The progress bar is in:
function OnGUI()
Thanks.
Answer by ShinyTaco · Jan 31, 2013 at 01:43 PM
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
Answer by Dave-Carlile · Jan 30, 2013 at 01:52 PM
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);
Hey, thanks for the help. I tried:
barDisplay = barDisplay - x;
if (barDisplay < 0) barDisplay = 0;
and
barDisplay = $$anonymous$$athf.Clamp01(barDisplay - x);
Still didn't work.
"Didn't work" isn't really helpful for us. What didn't work? What happened?
The timer just kept going, the game went on like normal.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091844im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Load bar in GameObject 2 Answers
progress bar 1 Answer
How to create a vertical circular progress bar 1 Answer
Problem ProgressBar with health + Time the poison 1 Answer
Progress of loading level 1 Answer