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 /
avatar image
0
Question by ThugsInMugs · Aug 03, 2016 at 09:28 PM · c#guicoroutineloopwaitforseconds

Why isn't my simple coroutine working? (and how can I make it infinite?)

So far there is no errors and the text displays properly on the GUI but it only displays the starting amount even though I have a Coroutine that updates that amount, heres what I have so far(c#):

 public class Money : MonoBehaviour {
 public float cashInInventory = 100;
 public float cashEarnAmount = 50;
 public Text spendingCash;

  void Start () {
      spendingCash = GetComponent<Text>();
      StartCoroutine("GiveCash");
  }
  private IEnumerable GiveCash()
  {
      while (true)
      {
          yield return new WaitForSeconds(5.0f);
            cashInInventory += cashEarnAmount;  
      }
  }
  
  // Update is called once per frame
  void Update () {
      spendingCash.text = cashInInventory.ToString("f0");
      print(cashInInventory);
  }
 }
 





Comment
Add comment · Show 1
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 tqkiettk10 · Aug 05, 2016 at 07:03 AM 0
Share

If you want repeat function I think you should use InvokeRepeating void Start () { spendingCash = GetComponent (); InvokeRepeating("GiveCash", 0, 5.0F); }

private void GiveCash() { cashInInventory += cashEarnAmount;

}

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html

2 Replies

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

Answer by lunoland · Aug 04, 2016 at 05:05 AM

StartCoroutine("GiveCash") would work too, the issue is the return type not being IEnumerator.

You don't need to use a coroutine though, for something this simple you can use Time.time:

 float lastCashEarnedAt = 0f;
 
 void Update() {
     if (Time.time - lastCashEarnedAt >= 5f) {
         GiveCash();
     }
 }

 void GiveCash() {
     cashInInventory += cashEarnAmount;
     lastCashEarnedAt = Time.time;
 }

Comment
Add comment · Show 4 · 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 ShawnFeatherly · Aug 04, 2016 at 08:12 AM 1
Share

Using the coroutine would give better performance. WaitForSeconds(5.0f) is more performant than doing an if check every frame.

avatar image lunoland ShawnFeatherly · Aug 04, 2016 at 07:07 PM 0
Share

Oh is it really? It's not indicated in the docs, but I would assume that WaitForSeconds must ultimately result in some similar polling to deter$$anonymous$$e when to resume the coroutine. I'm genuinely curious to know if that's not the case.

Regardless, the difference is trivial (that check is not costly), and possibly cuts both ways: Does the performance gained from WaitForSeconds outweigh the overhead of using a coroutine?

Ins$$anonymous$$d of speculating, I would suggest that everyone use the method they find most clear/maintainable, and follow the conventional wisdom of optimizing for performance only after there's an issue.

avatar image flaviusxvii lunoland · Aug 05, 2016 at 04:36 AM 0
Share

Coroutines have virtually no performance overhead.

From here: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

WaitForSeconds isn't a busy wait. It hands the thread back and set's a timer for future invocation.

avatar image ThugsInMugs · Aug 04, 2016 at 07:34 PM 0
Share

Thank you! I added this and some other stuff to get a nicley working cashflow script!

avatar image
3

Answer by flaviusxvii · Aug 03, 2016 at 10:00 PM

StartCoroutine("GiveCash"); should be StartCoroutine(GiveCash());

And I'm pretty sure you want GiveCash() to return IEnumerator instead of IEnumerable.

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

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

7 People are following this question.

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

Related Questions

C# simple delay execution without coroutine? 2 Answers

I'm having trouble with Coroutines.. 2 Answers

Executing coroutines consecutively 0 Answers

Coroutine gets stuck randomly? 1 Answer

Distribute terrain in zones 3 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