[SOLVED] Existed prefab countdown timer bar resetting it's value back to begining after i instantiate a new one.

So i had countdown timer bar prefab which instantiated in vertical layout group. It’s work perfectly fine for the 1st time, the timer will reducing as it should be. But when i instantiate more, the old one will resetting it’s value back to beginning.

public class CountDownTimerBar : MonoBehaviour
{
    float maxTime;
    public static float runningTime;
    Image timerBar;

     void Awake()
    {
        maxTime = PlayerPrefs.GetFloat("SetPartyEsc");
        runningTime = maxTime;
        timerBar = GetComponent<Image>();
    }

    void Update()
    {
        if (runningTime > 0)
        {
            runningTime -= Time.deltaTime;
            timerBar.fillAmount = runningTime / maxTime;
        }
    }
}

This is my instantiate script :

public GameObject cardPrefab;
    public GameObject menu;
    public float addHeigthValue = 190f;
    private VerticalLayoutGroup layout;
    private RectTransform rt;

    void Start()
    {
        layout = GetComponent<VerticalLayoutGroup>();
        rt = GetComponent<RectTransform>();
    }

    public void AddaCard()
    {
        if(rt.transform.childCount < 4)
        {
            Instantiate(cardPrefab, layout.transform);
            CloseMenu();
        }
        else if(rt.transform.childCount >= 4)
        {
            Instantiate(cardPrefab, layout.transform);
            rt.sizeDelta = new Vector2(rt.sizeDelta.x, (rt.sizeDelta.y + addHeigthValue));
            CloseMenu();
        }              
    }

    public void CloseMenu()
    {
        menu.gameObject.SetActive(false);        
    }

Here’s some images…
It’s working for 1st instantiate :

alt text

But when i instantiate another one…

alt text

Can anyone help me with this ?

Hello there,

Please correct me if I’m wrong, but it seems your “runningTime” variable is declared as a static variable.

This means that it will be shared between all your instances, instead of each one having their own.

To fix this, removing the “static” keyword should be enough.


Hope that helps!

Cheers,

~LegendBaocn