Progress bar Help!

Hi guys,

I have been making a script for a progress bar and it works fine other than it starts in the centre then pushes out left and right. I want the bar to start left and move right till it fills then let the button spawn and be click able. any help would be great.

My script:

#pragma strict

public var player: Player;

private var currentProgress:double = 0;
private var caseValueM:double = 1.50;
private var caseValueF:double = 1.50;

public var solveButtonActive:boolean = false;
public var solveButton:GameObject;
public static var solveButtonPressed:boolean = false;

function Start () {

}

function Update ()

{
transform.localScale = Vector3(currentProgress/200,0,0.1);
if (currentProgress == 100 && !solveButtonActive)
{
Instantiate(solveButton);
solveButtonActive = true;
}
if (solveButtonPressed)
{
solveButtonPressed = false;
currentProgress = 0;
solveButtonActive = false;
player.updateMoney(caseValueM);
player.updateFame(caseValueF);
}
}

public function progress(progression:double)
{
if (currentProgress < 100){
currentProgress += progression;
}
}

Second script

#pragma strict

public var caseProgressor:CaseScript;

function Start () {

}

function Update () {

}

function OnMouseDown () {
caseProgressor.progress(5);
}

Hello.

You are not using GUI elements here if I understand…

I think the best way to do a ProgressBar is a simple GUI elements. I’m not familiar with Unity 4.6 GUI but I often use the GUI class and functions in my MonoBehaviours to display my 2D UI.

To achieve a progressbar, this is really simple with this class ! Here is a sample of how to draw a ProgressBar in GUI (C#) :

    private static GUIStyle ProgressBarBackground = new GUIStyle();
    private static GUIStyle ProgressBarContent = new GUIStyle();
    private bool ProgressBarStylesInit = false;

    public static void InitStyles() {
        // Put here your styles values;
        ProgressBarStylesInit = true;
    }

    public static void ProgressBar(float _width, float _height, int _value, int _max, params GUILayoutOption[] _options) {
        if (!ProgressBarStylesInit)
            InitStyles();
  
        _value = Mathf.Clamp(_value, 0, _max);
        GUILayout.BeginHorizontal(_options);
        GUILayout.Label("", ProgressBarBackground, GUILayout.Width(_width));
        GUILayout.Space(-_width - ProgressBarBackground.margin.left);
        GUILayout.Label("", ProgressBarContent, GUILayout.Width(Mathf.CeilToInt(_width * _value / (float)_max)));
        GUILayout.Space(_width - Mathf.CeilToInt(_width * _value / (float)_max));
        GUILayout.EndHorizontal();
    }

When you know how to use the GUI class, this become simple to add a button when it’s full, and do what you want.

Hope this helps

1 Like