Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

yield StartCoroutine() woes

Discussion in 'Scripting' started by StarManta, Dec 10, 2006.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    Alright, I've got two lines:

    Code (csharp):
    1.     yield StartCoroutine("Say", ["Welcome to the game!"]);
    2.     Say(["Build a robot"]);
    Say takes a String[] as its parameter. The bottom line works fine, although it does not wait until the Say method is done before marching on. The top line gives me the error: "The best match for method Say has some invalid parameter"

    I also tried:
    Code (csharp):
    1. yield Say(["Build a robot"]);
    And it then started giving me some errors I didn't understand about "generators" and IENumerable....

    What I'm trying to accomplish is to have the Say function do things over a certain period of time, and then return, and have the main code section just wait until Say has done its business. What's the "correct" way to do this in JS?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    How is the Say function declared? What parameters does it take?
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    I think I've stumbled upon a solution that works, although I don't understand why this one works where it didn't before. Is there documentation on how to correctly use "yield" anywhere?

    Code (csharp):
    1. function Start () {
    2.     Subtitle = transform.Find("Subtitle").gameObject;
    3.    
    4.     yield Say(["Welcome to the game!"]);
    5.     yield Say(["Build a robot"]);
    6.     yield Say(["Make stuff happen"]);
    7. }
    8.  
    9. function Say(data: String[]) {
    10.     var pauseTime = defaultSubtitleTime;
    11. //does a bunch of other stuff
    12.     yield WaitForSeconds(pauseTime);
    13. }
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can use both
    yield StartCoroutine ("TheFunction");

    and yield TheFunction ();

    from JavaScript it is recommended to use yield TheFunction (); since it is faster, and allows you to pass multiple parameters. For C# you have to go through StartCoroutine instead of the direct function call