Waiting for a Function to Complete

Hi,

I have a script which reads a text file a line at a time and then outputs that line a character at a time, teletype style.

import System.IO;
var textAsset : TextAsset;
var letterPause = 0.2;
var linePause = 10;
var sound : AudioClip;
private var word;

function Start() {
    readFile();
}

function readFile() {
    if (textAsset == null) return;
    reader = new StringReader(textAsset.text);
    line = reader.ReadLine();
    while (line != null)
    {
        yield WaitForSeconds(linePause);
        word = line;
        TypeText();
        line = reader.ReadLine();
    }
}

function TypeText () {
        for (var letter in word.ToCharArray()) {
        guiText.text += letter;
        if (sound)
            audio.PlayOneShot (sound);
        yield WaitForSeconds (letterPause);
    }
    guiText.text += "
";
}

This works, but by using the yield WaitForSeconds it's limited by being fixed. I wanted to try to pause the readFile() function until the TypeText() has finished outputting the line. So, I did this:

import System.IO;
var textAsset : TextAsset;
var letterPause = 0.2;
var linePause = 10;
var sound : AudioClip;
private var word;
private var inProgress : boolean = false;

function Start() {
    readFile();
}

function readFile() {
    if (textAsset == null) return;
    reader = new StringReader(textAsset.text);
    line = reader.ReadLine();
    while (line != null)
    {
            if(inProgress == false){   // also tried this as if(!inProgress)
            word = line;
            TypeText();
            line = reader.ReadLine();
            }
    }
}

function TypeText () {
    inProgress = true;
    for (var letter in word.ToCharArray()) {
        guiText.text += letter;
        if (sound)
            audio.PlayOneShot (sound);
        yield WaitForSeconds (letterPause);
    }
    guiText.text += "
";
    yield WaitForSeconds(2);
    inProgress = false;
}

When I try to run the game then, Unity just freezes. I get the initial dimming, but the button never goes blue and the game view never comes up. Unity locks up completely and I have to force it to quit.

What am I missing or not understanding here?

[edit] Oh, and no error messages. Also Windows 7 and Unity Free.

tia

If you want to wait until a coroutine finishes you could try to use this in your first script:

yield TypeText();