- Home /
The question is answered, right answer was accepted
[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 :
But when i instantiate another one...
Can anyone help me with this ?
How do you do the instatiation? If by script you better post that code.
Answer by Legend_Bacon · May 08, 2019 at 09:50 AM
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
Oh wow, thank you it's work ! Reason i use static because i want to use that value for lerping in another script. Thank you for your answer.
Follow this Question
Related Questions
Source Gameobject rendered when using prefab 0 Answers
Custom Inspector: Using SeralizedProperty changes the prefab values! 0 Answers
Instantiating prefabs creates a clone, not instance of the prefab 1 Answer
"Unable to instantiate prefab. Prefab may be broken" 7 Answers
prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer