- Home /
Progress Bar problem
I know it's asked a lot because I've looked around for answers but can't quite fix this issue.
It's the same code you have probably seen floating around before and I can't work out how to convert it from filling up with time to filling up when pickups are picked up.
var pickupTarget : int;
var currentPickups : int;
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(120,40);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
var skin : GUISkin;
function Start()
{
GUI.skin = skin;
pickupTarget = 100;
}
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()
{
// for this example, the bar display is linked to the current time,
// however you would set this value based on your desired display
// eg, the loading progress, the player's health, or whatever.
barDisplay = currentPickups/pickupTarget;
if (barDisplay >=1)
{
barDisplay =1;
}
}
I've tried a few things with the update but i'm clutching at straws. When the player picks up an object it adds one to the 'current pickups'. I want the bar to fill when 'pickuptarget# has been reached (when 100 pickups are collected the bar is full). Also there are over 100 pickups so thats why i have that limiter thing at the very end.
what glaring obvious mistake have I made here? thanks guys!
Answer by MikeErty · Oct 30, 2013 at 12:33 PM
I worked out the solution was to change my currentPickup and target vars to floats, change the amount of points added per pickup in the pickup script (to 0.1 in this case) and then it worked it out properly. I'm guessing the maths behind what I was originally doing was a bit mental. I can't visualise mathematics very well, I'm more words and pictures :D
Your answer
Follow this Question
Related Questions
How to write text or bars in a 3d scene?? 3 Answers
Heath Bar and Enemy 1 Answer
NGUI UI Slider: Progress Bar 1 Answer
Loading Bar Until Ad Show Up 0 Answers
How do i add a healthbar/actionbar gui 4 Answers