Search Unity

Yielding Not Working in a "for" Loop?

Discussion in 'Scripting' started by SomeGuy22, Sep 6, 2011.

  1. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Basically I have a loop in the start () function like so:

    Code (csharp):
    1.  
    2. var add = ["T", "e"];
    3. var stop = 1.0;
    4.  
    5. function Start() {
    6.     var myTextMesh = (GetComponent(TextMesh) as TextMesh);
    7.     myTextMesh.text = "";
    8.     for (i = 0; i < add.length; i++)
    9.     {
    10.         myTextMesh.text += add[i];
    11.         yield WaitForSeconds (stop);
    12.     }
    13. }
    14.  
    I'm trying to add the letters, "T" and "e" using a loop, but whenever I start the game, it just adds "T".

    After Debug.Logging, I removed the yield statement, and it added both letters perfectly.

    Does anyone know why this yield statement is acting up?

    Thanks in advance!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nothing wrong with the code; works fine here.
    --Eric
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Really? Might be a compile error.

    Maybe I should re-import all?

    Any suggestions?
     
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Anyone?
     
  5. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    did you pause the game? (time.timeScale = 0?)
    if so, than its because of that.
     
  6. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Brilliant idea!

    Thanks it worked perfectly!!!
     
  7. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    glad i could help ^^
     
  8. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    I really think it'd be worth unity coming with something like WaitForRealtimeSeconds.