Increment variable over time

hi. I have this small problem to do some automatic update on a guitext. what i want to do is, to update a guitext after 3 seconds constantly until the game finish. the value would be incrementing one by one after 3 seconds.

Class guitextautoupdate{
    private int _i;
    System.Collections.IEnumerator yieldConnect() {         
        _i++; 
        Guitext.text = _i.ToString();
        yield return new WaitForSeconds(3);             
    }

    void Start()
    {
        StartCoroutine(yieldConnect( )); 
    }
}

What i think is, this code will update the text after 3 seconds. but it doesnt do it.

so how do i do that?

I generally find InvokeRepeating best for this sort of thing.

private int _i = 0;
void YieldConnect() {
    guiText.text = (++_i).ToString();
}

void Start() {
    InvokeRepeating ("YieldConnect", 3.0f, 3.0f); 
}

Use `CancelInvoke` to make it stop. Note that the correct spelling is `guiText`, not `Guitext`.

Something like this should do it:

Class guitextautoupdate{
    private int _i;
    System.Collections.IEnumerator yieldConnect() {  
        while(true)
        {
            _i++; 
            Guitext.text = _i.ToString();
            yield return new WaitForSeconds(3);
        }          
    }

    void Start()
    {
        StartCoroutine(yieldConnect( )); 
    }
}

You just need to make your coroutine have a while loop if you want it to go forever:

IEnumerator yieldConnect()
{
     while(true)
     {
          // your code
          yield return new WaitForSeconds(3);
     }
}

You can also use a condition, like `while(i < 10)` or something like that. You can also break out of the while loop (and thus, end the coroutine), with the `break;` keyword.

How can one do this in javascript for Unity? I've tried, but as a novice coder, translating c# to JS is a little out of my league somewhat. Thanks.