Inconsistent yield/For loop behaviour

I’ve made up a little typewriter function to progressively modify the text field of a 3D text object, but am coming across some strange inconsistencies.

When running this in the editor or a Windows deployment, perhaps one in five times there is a four second stall between writing the first character in and typing out the rest of the string. Then, when running this in the web player, the function stops running completely after the second text object has been modified. I could understand if some oversight in my code led to a consistent failing, but this has me baffled.

Any help would be much appreciated.

    function TypeBoasts(){
        active123 = true;
        var title = "I can do it . . .";
        for (var p = 0; p<title.length; p++){
            textMeshes[0].text += title[p];
            yield WaitForSeconds (0.1);
        }
        var no1 = "1)";
        for (var l = 0; l<no1.length; l++){
            textMeshes[1].text += no1[l];
            yield WaitForSeconds (0.1);
        }
        var firstBoast = Globals.boastText[Globals.firstTo14];
        for (var i = 0; i<firstBoast.length; i++){
            textMeshes[4].text += firstBoast*;*

yield WaitForSeconds (0.1);
}
//… through several more objects until complete
}

This smells like several coroutines being executed in parallel. Usually a boolean is used to avoid this:

private var running = false; // declare this outside any function

function TypeBoasts(){
    running = true; // coroutine running now
    ...
    // end of coroutine: clear boolean
    running = false;
}

// caller code: only start the coroutine if it's not already running

if (!running) TypeBoasts();

But your variable active123 seems to play this role - is this true? If so, check the code carefully: the caller code may have some error that allows multiple calls, or active123 may be being reset before the coroutine finishes.

Another possibility is this code be attached to more than one object, what wouldn’t block multiple coroutines because a local active123 exists in each script instance. A simple way to check this is to make the variable static: this way the boolean is the same for all scripts, thus multiple TypeBoasts won’t be called:

static var active123 = false;

I’ve found the problem!

At some point it would seem that I inadvertently deleted the #pragma strict at the head of this script. Adding it back in led to a few new errors being flagged, specifically:

'length' is not a member of 'Object'
    and
Type 'Object' does not support slicing

With these, the solution was clear. I simply had to type my dynamic strings (i.e. firstBoast) when declaring them. The corrected block of code is as follows (though, as Eric5h5 says, it would still be better off as a freestanding function):

var firstBoast : String = Globals.boastText[Globals.firstTo14];
for (var i = 0; i<firstBoast.length; i++){
textMeshes[4].text += firstBoast*;*

yield WaitForSeconds (0.1);
Thanks again, all, for your help. I certainly wouldn’t have found it if you hadn’t encouraged me to look around. And I learned a great deal!