Coroutines, yield, c#... i don't get it!

OK I’m feeling kind of silly for not figuring out out to use the yield with coroutines and waitforseconds…

I would really appreciate if somebody could explain how to achieve this, in C# code:

void Update()
{
  if(condition)
    handleSomething();
}

void handleSomething()
{
  // do some things

  // wait for some amount of time (variable)

  // do some more things

}

I’ve tried with the StartCoroutine inside my handleSomething, then yield return new WaitForSeconds (??) in the coroutine, but it doesn’t behave correctly.

Anyone? Thanks!

joe@threemelons

I believe you want this:

void Update(){
if( condition )
    StartCoroutine( "handleSomething" );
}

IEnumerator handleSomething(){
    // do something

    yield return new WaitForSeconds( 2.5f );

    // do something else
}

Check the docs:
http://unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
http://unity3d.com/Documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html

Regards,
Afonso

you were usefull nafonso, thanks :slight_smile: