How to Loop Progress Bar each time variables change ?

Hi guys,

I’ve a player who collects Stars. Each time player collects 16 Stars, protection effect will be active for 5 secs. I created a progress bar with Slider component to let player know when effect is gonna be active.

So i created two different variables named; " float countStars" and “float awakeEffect”.

Where i stuck is; yes countStars will increase but awakeEffect will increase too so progress bar will be broke after first time cuz ratio of them will change…

To summarize it; how can i loop this process ?

Can you help me to solve this problem? Thanks…

    public GameObject protectionEffect;
    public Slider progressBar;

    public float countStars = 0;
    public float awakeEffect = 16;

    private void Awake()
    {
        Instantiate(protectionEffect, transform.position, Quaternion.identity);
    }

    private Start()
    {
        protectionEffect.SetActive(false);
    }

    private void Update()
    {
        if (countStars >= awakeEffect)
        {
            awakeEffect += 16;
   
            protectionEffect.SetActive(true); // I activate it for 5 seconds...
        }

        progressBar.value = (countStars * 1f) / (awakeEffect * 1f); //Here is where i stuck on...
        // when countStars reaches 16 then awakeEffect will be 16+16 = 32.
       // so ratio of them will be 0.5 and keep changing...
       // I want it to go back to zero and start over...
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Star")
        {
            countStars++;
        }
    }
}

Just reset countStars to 0. You don’t need the awakeEffect variable at all. You don’t even need to use Update for this as there is no need for Unity to call code every frame.

1 Like

@WarmedxMints_1 I can’t reset countStar cuz i determine something else by this variable. I better create a new variable which i can reset. Where else can i call this function instead of Update().