- Home /
Question by
ClementFeanor · Mar 18, 2019 at 08:21 AM ·
imagetimerreset
Reset a Timer made with .fillAmount
Hello ! I'm currently working on my degree project, and I have this little script which allow me to make a visual timer out of an image :
public class TimerButton : MonoBehaviour {
Image timerButton;
public float maxTime = 1.5f;
public float timeLeft;
public GameObject Button;
void Start () {
timerButton = GetComponent<Image>();
timeLeft = maxTime;
}
void Update () {
if (timeLeft > 0)
{
timeLeft -= Time.deltaTime;
timerButton.fillAmount = timeLeft / maxTime;
}
}
}
I'd like to know how I can reset the timer while the Game Object who carries the image isn't active, so that as soon as the Game Object is active again, the timeLeft value equals the maxTime value. Does it have to be in a separate script ?
Thanks a lot for your help !
Comment
Best Answer
Answer by xxmariofer · Mar 18, 2019 at 08:35 AM
you can use the OnEnable method for that
void OnEnable(){ timeLeft = maxTime; }
Thanks a lot ! I'm quite a newbie here, really appreciate this help!