Hello, I’m trying to use NGUI’s UI Slider to make a progress bar for a game that decreases as time passes when harvesting minerals and resources. The problem is that the bars don’t have a fixed maximum size to decrease from. I wanted all of the progress bars to be uniform despite having different times, but they all instead become longer the more time there is. How do I fix this?
The Bar Code:
public class Harvest_Bar : MonoBehaviour {
private UISlider _slider;
private float _maxWidth;
private float _balancer;
void Awake ()
{
_slider = GetComponent<UISlider>();
_maxWidth = _slider.foreground.localScale.x;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void updateDisplay(float totalTime)
{
_slider.foreground.localScale = new Vector3(totalTime * 2.56f, _slider.foreground.localScale.y, _slider.foreground.localScale.z);
}
}
Code calling it:
void Update()
{
//Timer
//guiText.text = "Time Left: " + Mathf.Floor(activeTime);
if(isHarvesting || isReplinishing)
{
activeTime -= Time.deltaTime;
pb.GetComponent<Harvest_Bar>().updateDisplay(activeTime);
}
if(activeTime <= 0)
{
if(isHarvesting)
{
harvestComplete();
}
else if (isReplinishing)
{
replinishComplete();
}
}
}