I would like to call a function every second or so. It seems the best method for this is to do a StartCoroutine, but I’m unsure if calling StartCoroutine() ‘recursively’ is a bad idea:
IEnumerator pollForTweetability()
{
yield return new WaitForSeconds(1.0f);
setButtonEnabled(photoController.canUserTweet());
StartCoroutine(pollForTweetability());
}
I’m new to Unity and C#. Is this the preferred way to poll and execute code?
You could use the Update function that’s called every frame or second i forgot which one
In this instance I’d prefer not to call Update, but I understand that’s an option. Update is called every frame and so its a little CPU-heavy for what I’m doing.
Calling it recursively would be a bad idea, since eventually you’d run out of stack space.
while (true) {
yield return new WaitForSeconds(1.0f);
setButtonEnabled(photoController.canUserTweet());
StartCoroutine(pollForTweetability())
}
Or just use InvokeRepeating instead.
–Eric
Thanks Eric, I don’t know why I didn’t see InvokeRepeating. That is exactly what I’m after!