OnGUI doesn't update

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.
87217-meter-air-copy.png

empty time progress.
87218-meter-background.png

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();
		}
        }
}

This line:

var nW = ((bgW - iconWidth) * percent) * iconWidth;

doesn’t make much sense. You calulate the width of the background minus the iconWidth which gives you the width of the remaining background. That amount you scale with your percentage. Now comes the strange thing, why do you multiply by iconWidth? You should remove that last multiplication.

Also you seem to only scale your image instead of actually “sliding” it. Shouldn’t you use that variable as x-offset?

It would help to understand how your actual images look like (timeBarTexture and bgTexture). Also what exact behaviour you want. If the time is 10, should the bar be “full”? While it’s decreasing, should it move to the right? In that case you have to invert your percentage.

Basically something like this:

void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
    var bgW = background.width;
    var bgH = background.height;

    GUI.BeginGroup (new Rect (x, y, bgW, bgH));
    GUI.DrawTexture (new Rect (0, 0, bgW, bgH), background);
    
    var xPos = ((bgW - iconWidth) * (1f-percent);
    GUI.DrawTexture (new Rect (xPos, 0, bgW, bgH), texture);
    
    GUI.EndGroup ();
 }