Unity3d progress bar

14,599

Solution 1

Create a UI Image that is of type Filled. Use horizontal or vertical fill depending on your progress bar. Then from inside a script you can manipulate the value of the image. I will give you a very simple c# example. For the rest you can just use google and read the unity scripting API.

public class Example: MonoBehaviour {

    public Image progress;

    // Update is called once per frame
    void Update () 
    {
            progress.fillAmount -=  Time.deltaTime;
    }
}

Solution 2

U could use a slider en slider.setvalue.

Example:

//to use this add first a slider to your canvas in the editor.
public GameObject sliderObject; //attachs this to the slider gameobject in  the editor or use Gameobject.Find
Slider slider = sliderObject.GetComponent<Slider>();
float time;
float maxtime; //insert your maxium time

void start(){
    slider.maxValue = maxtime;
}

void update()
{
    time += Time.deltaTime;
    if (time < maxtime)
    {
        slider.value = time;
    }
    else
    {
        Destroy(sliderObject);
        //time is over, add here what you want to happen next
    }
}
Share:
14,599
A.Njuguna
Author by

A.Njuguna

Updated on June 08, 2022

Comments

  • A.Njuguna
    A.Njuguna almost 2 years

    I am developing a game using unity3D and I need help making a progress time bar such that on collecting particular items time is added and thus the game continues.