- Home /
how to I reset a health or progress bar
Hi I am stuck and pls need some help. My game is a wordsearch puzzle which has a progress bar. when the bar is filled, the game ends and the player is expected to have completed the game. The problem is that when the player clicks on the replay button the progress bar doesnt reset like a normer Timer would do. Any ideas pls?
Some code would be helpful. How do you declare your progress bar variable, what do you do to reset the game, etc.?
Answer by Statement · Dec 06, 2010 at 11:19 PM
Based on your previous information:
if (buttonWasClicked)
{
progressBar.progess = 0;
}
Based on your update with the link, I am guessing that you want it to be like this:
var barDisplay : float = 0; var pos : Vector2 = new Vector2(20,40); var size : Vector2 = new Vector2(60,20); var progressBarEmpty : Texture2D; var progressBarFull : Texture2D;
function OnGUI() {
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function Update() { barDisplay += Time.deltaTime * 0.05; barDisplay = Mathf.Clamp01(barDisplay); }
function ResetBar() { barDisplay = 0.0f; }
You can then control the bar by setting enabled = true when you want the bar to tick up, and call ResetBar() when you want it to reset (or just modify barDisplay, it runs between 0 and 1).
Thank you for your response.I got help for the creation of my progress bar from this link:http://answers.unity3d.com/questions/4456. $$anonymous$$aybe this could help. I did try something similer to your answer but still didn't get it to work.