• 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.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How do I access variables from a game object from an instantiated prefab 1 Answer

Dynamic Scene Creation through Scripts? 1 Answer

How to obtain this prefab name in this circunstances 1 Answer

Bool wont change on prefab clones 1 Answer

Can't modify BoxCollider center/size on base prefab 0 Answers


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