- Home /
Question by
username707 · Jul 30, 2013 at 10:07 AM ·
c#coroutineyield
How To cycle coroutine in C# say every 1 sec?
as above For example i need this to run every second
void Start () {
thisTransform = transform;
StartCoroutine(onCoroutine());
}
IEnumerator onCoroutine()
{
Debug.Log ("OnCoroutine: "+Time.time);
yield return new WaitForSeconds(1f);
}
Comment
Please accept one of our answers, if one of them has given you the proper solution to fit your needs.
Best Answer
Answer by clunk47 · Jul 30, 2013 at 03:19 PM
Just use while(true), and have your yield inside of the while(true) statement. I'd also cast Time.time to an int.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void Start()
{
StartCoroutine (onCoroutine());
}
IEnumerator onCoroutine()
{
while(true)
{
Debug.Log ("OnCoroutine: "+(int)Time.time);
yield return new WaitForSeconds(1f);
}
}
}
Answer by Lovrenc · Jul 30, 2013 at 10:14 AM
IEnumerator onCoroutine()
{
while(continueCoroutine) { //variable that enables you to kill routine
Debug.Log ("OnCoroutine: "+Time.time);
yield return new WaitForSeconds(1f);
}
}
nice, seems like a good idea to put the variable there regardless of whether its needed or not, to be able to easily decide to stop it later :)
No need you can stop coroutines properly with StopCoroutine