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 /
  • Help Room /
avatar image
0
Question by martipello · Feb 21, 2016 at 12:11 AM · intcountdownwhile

counting down a variable issue

hi complete noob here lets get to it, my player collects coins. At the end of the level it displays how many coins were collected. The number of coins will be added to the total score. An animation of the numbers decreasing in the text ui box is what i want, ive achieved this from this question here however for some reason it shows the amount of coins that were collected just fine and then counts down to -114 is there something im missing?

here is my script updating the text

 [SerializeField]
 Text coinText;
 [SerializeField]
 globals coinScript;
 [SerializeField]
 public float speed;
 public int countdown;

 void Awake()
 {
     countdown = globals.allCoinsCollected;
     if (coinText != null && coinScript != null)
     {
         coinText.text = countdown.ToString();

     }
 }
 IEnumerator MyCoroutine()
 {
     
     while (countdown >= 1)
     {
         yield return new WaitForSeconds(1.0f);
         countdown--;
     }
 }
   
 void Update()
 {
     StartCoroutine("MyCoroutine");
     coinText.text = countdown.ToString();
 }

}

and the text and global script are set in the editor, as earlier mentioned. It displays the correct amount of coins collected but then counts down to -114

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jgodfrey · Feb 21, 2016 at 12:35 AM

That bare call to "StartCoroutine" in Update is likely problematic. I assume you don't want to start the coroutine on every frame, right? Depending on your needs, you probably want to start the coroutine from Awake/Start. If you need to start it from Update for some reason, you probably only want to start it once. Certainly, not every frame...

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 martipello · Feb 21, 2016 at 09:48 AM 0
Share

that was exactly it thank you very much! must have been sleep drunk or something

avatar image
1

Answer by codeman_nz · Feb 21, 2016 at 10:10 AM

Move you Update code to Start and that should solve your issue.

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 martipello · Feb 21, 2016 at 12:53 PM 0
Share

Yeah solved it i was working too late i think lol thank you :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

35 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

Related Questions

Unity crashes when using while loop 0 Answers

Countdown timer for starting game 0 Answers

Switch Case help 0 Answers

Do a smaller object type than int exists? 2 Answers

Spawn a number of prefabs an equal distance apart 2 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