- Home /
The question is answered, right answer was accepted
UI Decreasing Timer Bar Not Executing Print?
I have this UI timer bar that decreases in size only when it reaches the end it does not seem to print the "You Loose" statement. I'm missing something simple I'm sure but just not seeing it. I forgot to mention my Bar is just a UI image set to "Fill" from Left to Right" if that helps, the bar works and decreases the fill amount only it;s not doing the last part of the script right. I can find a way around getting the whole screen to do what I need with another timer function set to a certain time and then have it do what I need but that is more complicated than it needs to be when just this simple script should do the job.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class BarHandler : MonoBehaviour {
public Image DeathTimerBar;
public float DecreaseAmount;
void Update (){
DeathTimerBar.fillAmount -= DecreaseAmount * Time.deltaTime;
if (DeathTimerBar.fillAmount >= 1f) {
print ("You Loose!");
this.enabled = false;
}
}
}
Well on my phone so I could have missed something but it looks O$$anonymous$$ to me why not print the value of your DeathTimerBar.fillAmount before the if as debug check.
Answer by tigertrussell · Mar 04, 2015 at 02:54 PM
(edited to remove stupid question about print
wrapper)
It looks like you're subtracting from fillAmount
and then expect it to print when it is >= 1f
, shouldn't that be something like <= .05f
or something closer to zero?
Print is a wrapper around Debug.Log in $$anonymous$$onoBehaviour, they should be ok using it as the script inherits from mono Although I use debug myself.
Thanks tigertrussell, yup, that was what I was doing wrong... I usually do use the Debug.Log but the guy on YouTube who had the tutorial was using the print, plus I tweaked the code to run the fill in reverse of what he had in his tutorial, but the last part of the script was confusing me. Now it works so I can make it do something once it gets to the end now.
On Another not I wonder if I can use what you have done here to also change the colour of the fill bar when it gets to a certain level and even add a voice clip or warning siren.? I think I have a pretty good idea of how to do that with the render.material stuff.
This is cool, thanks guys, this was a BIG help :)
Yup, As I suspected I can just add some If Statements and get the exact point of the bar I want to add in any sound or colour change... Cool :)
![void Update (){
DeathTimerBar.fillAmount -= DecreaseAmount * Time.deltaTime;
//Bar In Yellow Zone
if (DeathTimerBar.fillAmount <= 0.56f) {
Debug.Log ("You Are In The Yellow!");
}
//Bar In Red Zone
if (DeathTimerBar.fillAmount <= 0.32f) {
Debug.Log ("You Are In The Red!");
}
//Bar At End Now You Gonna Die $$anonymous$$utha Fuka
if (DeathTimerBar.fillAmount <= .01f) {
Debug.Log ("You All Out Of Air $$anonymous$$utha Fuka!");
this.enabled = false;
}
}][1]
[1]: /storage/temp/41819-screen-shot-2015-03-04-at-115106-am.png
Ha ha... ok this was kinda bizarre but yet potentially useful for distorting sounds, $$anonymous$$ay be useful for later, I put a playOneShot audio clip in the If part of the code and when it hit the yellow zone it platy the audio clip backwards, sounded like the aliens were invading. Now what I want but I could find a use for that effect later on for sure ;)
Lerping the pitch
is also a good way to get a time-dilation effect on sound :) Glad I could help!
Follow this Question
Related Questions
Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to animate RectTransform change in position 1 Answer
Options UI over all scenes 1 Answer