Search Unity

break yield

Discussion in 'Scripting' started by rastinrastini, Apr 5, 2011.

  1. rastinrastini

    rastinrastini

    Joined:
    Jul 8, 2010
    Posts:
    158
    hi
    i want break yield return new waitforseconds() in half of seconds.
    any one can help?
    thankful.
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Wouldn't a simple WaitForSeconds(0.5) work for that? WaitForSeconds can take Floats as well as Ints.
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    On this subject. You could build a "breaking" WaitForSeconds. See the attached code.

    Also, this code demonstrates the Coroutines, and feeding functions as variables.. and it works :)

    Code (csharp):
    1.  
    2. function Awake(){
    3.     StartCoroutine(UselessCoroutine());
    4. }
    5.  
    6. function UselessCoroutine(){
    7.     yield(WaitForSecondsX(1.5, testEvent));
    8.     print("Done");
    9. }
    10.  
    11. function WaitForSecondsX(seconds : float, event){
    12.     var breakTime=Time.time + seconds;
    13.     while(Time.time<breakTime){
    14.         yield;
    15.         if(event())return;
    16.     }
    17. }
    18.  
    19. function testEvent(){
    20.     return transform.position==Vector3.zero;
    21. }
    22.  
     
  4. rastinrastini

    rastinrastini

    Joined:
    Jul 8, 2010
    Posts:
    158
    with my script when
    yield return new waitforsecends()
    complete then my animation fade to idle animation.
    i want if state changed in waiting time then break yield and change animation to idle.
    thankful.
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    That would be what my example does. Wait for x seconds unless y event triggers.
     
  6. rastinrastini

    rastinrastini

    Joined:
    Jul 8, 2010
    Posts:
    158
    can write c# code?
    thankful.
     
  7. rastinrastini

    rastinrastini

    Joined:
    Jul 8, 2010
    Posts:
    158
    when i do this it jump from yield and run next code.
    can anyone help?
    thankful.