I have a coin magnet function in my game, this all works great, but I’m currently trying to add a ui slider that visually shows the player how much time they have left before the coin magnet power is up.
In the game, on my playerMotor script, when you hit a Magnet, this triggers the magnet code, and also the slider representation of the time you have left, this works, but only once, when you trigger another magnet power up, the timer is already at 0, how might I go about resetting the slider to perform the same function again and again each time it is enabled ? The following script is attached to the slider :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class magnetCountdown : MonoBehaviour {
// Magnet Time Counter - This Value MUST Match magnetTimeLimit From playerMotor
[Header("Magnet Time Counter Property")]
public float magnetTimeCounter = 15.0f;
private Slider magnetSlider;
void Start () {
// set reference to slider
magnetSlider = GetComponent<Slider>();
magnetSlider.maxValue = magnetTimeCounter;
magnetSlider.value = magnetTimeCounter;
}
void Update () {
// decrement magnetTimeCounter float with time it takes to draw each frame if its above 0
if (magnetTimeCounter > float.Epsilon) {
magnetTimeCounter -= Time.deltaTime;
}
else {
// If the values so low, just set to 0
magnetTimeCounter = 0;
}
// Set the sliders value to magnetTimeCounter value
magnetSlider.value = magnetTimeCounter;
if (magnetTimeCounter == 0) {
magnetSlider.gameObject.SetActive(false);
}
}
}
If it were me, I’d add an extra variable for “magnetTimeCounterFull” or something like that. Then, with 1 method like “ResetTimer” you can set the value of magnetTimeCounter to the new variable and also re-enable the slider game object. (based on your current code). I think that would let it re-run through its countdown.
1 Like
Thanks, that gave me an idea, and with 1 line, it’s working !
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class magnetCountdown : MonoBehaviour {
// Magnet Time Counter - This Value MUST Match magnetTimeLimit From playerMotor
[Header("Magnet Time Counter Property")]
public float magnetTimeCounter = 15.0f;
private Slider magnetSlider;
void Start () {
// set reference to slider
magnetSlider = GetComponent<Slider>();
magnetSlider.maxValue = magnetTimeCounter;
magnetSlider.value = magnetTimeCounter;
}
void Update () {
// decrement magnetTimeCounter float with time it takes to draw each frame if its above 0
if (magnetTimeCounter > float.Epsilon) {
magnetTimeCounter -= Time.deltaTime;
}
else {
// If the values so low, just set to 0
magnetTimeCounter = 0;
}
// Set the sliders value to magnetTimeCounter value
magnetSlider.value = magnetTimeCounter;
if (magnetTimeCounter == 0) {
magnetTimeCounter = 15.0f;
magnetSlider.gameObject.SetActive(false);
}
}
}
Just added magnetTimeCounter = 15.0f; into the magnetTimeCounter == 0 if statement.
Nice.
Then just set it active to let it run down , again
Glad ya got it sorted.
if I need both directions > and <? (magnetTimeCounter >< float.Epsilon)
No need to reply to a 4 year old post, if you got a error. Create a new one, it’s simple. Also you’ll have to give more information of your error, or what you need help with, I don’t reckon anyone here could read your mind, now can we 
Johan_Liebert123 impossible try another post cos this post solve a half of my problem, the slider…
This script make slider back to 0 slowly but only 1 to 0 means positive to 0, but not -1 to 0 or 0 to 1, is exclusive positive to 0, I need both sides and how to explaind another post in another post, better post here, logic.