Search Unity

WaitForSeconds problem

Discussion in 'Scripting' started by robhuhn, Feb 7, 2010.

  1. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    hi,

    i need some help with WaitForSeconds please. i try to set a 5 seconds timeout within the method Replay:

    Code (csharp):
    1.  
    2. private void onReplay()
    3. {
    4.     Debug.Log("onReplay");
    5.  
    6.     Replay();
    7. }
    8.  
    9.        
    10. private IEnumerator Replay()
    11. {
    12.     Debug.Log("before yield");
    13.  
    14.     yield return new WaitForSeconds (5);
    15.  
    16.     Debug.Log("after yield");
    17. }
    18.  
    Now I get the debug log "onReplay" and no further logs or any errors. I cant explain to myself why the method Replay wont be called. If i remove the line with yield and change the return type to void everything is fine and i get all logs.

    I would be glad for any help.
     
  2. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    Hi,

    Have a look at co-routines...

    StartCoroutine( Replay() );


    Regards,
    Matt.
     
  3. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    oh great. thats it. thank you very much matt! :)

    I do not understand how its pssible to block my method call anyway. So if anybody has some background knowledge and want to share it, that would be nice. But for now i am happy to have a working script.