Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get this timer for my progress bar?

I’m currently learning how to create progress bars but Ive run into a problem. Im not sure how I can reference the running timer in my CraftCopperBar script for my update. Or if i have the wrong idea please correct me.

public IEnumerator CraftCopperBar()
{
    while (copper >= copperBarValue)
    {
        button.SetActive(false);
        copper -= copperBarValue;
        yield return new WaitForSeconds(5f);
        copperBar += 1 * multiplier;

        if (copper < copperBarValue)
        {
            button.SetActive(true);
            break;
        }
    }

public void Update()
progressBar.fillAmount = (float)(x / 5f);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Upgrade to auto continuous timer in IEnumerator. This is a good way to solve your problem, In the following code, you no longer need the Update event for fill progress.

public IEnumerator CraftCopperBar(float waitTime)
{
    while (copper >= copperBarValue)
    {
        copper -= copperBarValue;
        button.SetActive(false);

        var timer = 0f;
        while (timer <= waitTime)
        {
            timer += Time.deltaTime;

            progressBar.fillAmount = timer / waitTime;
            
            yield return new WaitForEndOfFrame();
        }
        copperBar += 1 * multiplier;

        if (copper < copperBarValue)
        {
            button.SetActive(true);
            break;
        }
    }
}

Also put wait time in parentheses.

public void Start() => StartCoroutine(CraftCopperBar(5f));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading