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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

continue something if a key is still pressed down? 1 Answer

Count Down Timer C# Conversion Script Not Working Help 3 Answers

I need the timer to reset as soon as i hits 0 1 Answer

Way of Finding/accessing a variable from another script though another variable? 1 Answer

(17,37): error CS0841: A local variable `spawnPointIndex' cannot be used before it is declared 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges