I have a problem with time progress bar, It’s doesn’t update every moment. Just when time is less than zero it’s suddenly goes empty. It’s always full and doesn’t decrease. sorry for my terrible english.
full time progress.
empty time progress.
My code:
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public float time = 10;
public float maxTime = 10;
public float timeBurnRate = 1;
public Texture2D bgTexture;
public Texture2D timeBarTexture;
public int iconWidth = 32;
public Vector2 timeOffset = new Vector2 (10, 10);
void Start(){
player = GameObject.FindObjectOfType<Player>();
}
void OnGUI(){
var percent = Mathf.Clamp01 (time / maxTime);
if (!player)
percent = 0;
DrawMeter (timeOffset.x, timeOffset.y, timeBarTexture, bgTexture, percent);
}
void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
var bgW = background.width;
var bgH = background.height;
GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
var nW = ((bgW - iconWidth) * percent) * iconWidth;
GUI.BeginGroup (new Rect (x, y, nW, bgH));
GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
GUI.EndGroup ();
}
void Update () {
if (!player)
return;
if (time > 0) {
time -= Time.deltaTime * timeBurnRate;
} else {
Explode script = player.GetComponent<Explode>();
script.OnExplode();
}
}
}