Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 08, 2019 at 10:03 AM by Ikky333 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ikky333 · May 08, 2019 at 08:21 AM · prefab-instance

[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 ?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mchts · May 08, 2019 at 08:32 AM 0
Share

How do you do the instatiation? If by script you better post that code.

avatar image Ikky333 mchts · May 08, 2019 at 09:52 AM 0
Share

Oh sorry, 1st time posting here. I'm update it.

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ikky333 · May 08, 2019 at 10:02 AM 0
Share

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

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges