Public IEnumerator Coroutine call doesnt work..

Hi,

i have to classes "Button" and "Controller". In "TestA" i want to call a public function from "TestB" which has a coroutine included.

...Button-Class
private void ButtonDay()
    {
        print("ButtonDay pressed );
        controller.PlayAnimation();    //controller is instance of Controller
    }

//...Controller-Class
public IEnumerator PlayAnimation() 
    {
yield return StartCoroutine(WaitForSeconds(2.0F));
 // After waiting, do something ....
}

IEnumerator WaitForSeconds(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
    }

Unfortunately nothing works. The "PlayAnimation()"-function isnt called - no prints, error etc. Any hints on that? How do get this thing work?

Thanks, yosh

you need to use StartCoroutine to start it - may be best to add another function into the controller class to start the coroutine instead of doing it from the other class

something along these lines:

`
...Button-Class
private void ButtonDay()
{
    print("ButtonDay pressed" );
    controller.PlayAnimation();    //controller is instance of Controller
}

...Controller-Class public void PlayAnimation() { StartCoroutine(PlayAnim()); }

IEnumerator PlayAnim() {

yield return StartCoroutine(WaitForSeconds(2.0F));
// After waiting, do something ....

}

IEnumerator WaitForSeconds(float waitTime) { yield return new WaitForSeconds(waitTime); }

`

this way, you don't have to worry about ever forgetting StartCoroutine for your function

if you want it to work with chained yields, you could even make it return Coroutine instead of void, and return StartCoroutine(PlayAnim());

yeah its going to look something like this:

controller.StartCoroutine("MyCoroutine");

following in in controller class

private IEnumerator MyCoroutine(){yield return new WaitForSeconds(10)};