- Home /
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);
}
}
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
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;
}
Using the coroutine would give better performance. WaitForSeconds(5.0f) is more performant than doing an if check every frame.
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.
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.
Thank you! I added this and some other stuff to get a nicley working cashflow script!
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.
Your answer
Follow this Question
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