cancel yield WaitForSeconds

hi im making a scoreboard which shows each score separately using yield waitforseconds, i would like the user to be able to skip the yields, but don’t know how to cancel a yield whilst one is already happening.

currently im using if(skipMenu == false)yield waitforseconds and it works most of the time as my yields are only 0.5 apart, any help on how to make it work better though.

the two ways i have thought of so far is to have smaller yields of 0.1 and have five of them together with the if statement, or duplicate the script without the yields and just disable the script with the yields when the screen is touched

how bad are yields to use if i did use multiple ones together

Michael, from your further comments it seems you do want “wait for seconds or a tap”

Fortunately this is commonplace, here are the two routines:

private var __gWaitSystem:float;
function tappedWaitForSecondsOrTap()
    {
    __gWaitSystem = 0.0;
    }
function WaitForSecondsOrTap(seconds:float):IEnumerator
    {
    __gWaitSystem = seconds;
    while ( __gWaitSystem>0.0 )
        {
        __gWaitSystem -= Time.deltaTime;
        yield;
        }
    }

Here’s an example of how to use it:

function testMe()
    {
    Debug.Log("ONE");
    yield WaitForSecondsOrTap(5);
    Debug.Log("TWO");
    yield WaitForSecondsOrTap(5);
    Debug.Log("THREE");
    yield WaitForSecondsOrTap(5);
    Debug.Log("FOUR");
    yield WaitForSecondsOrTap(5);
    Debug.Log("FIVE");
    yield WaitForSecondsOrTap(5);
    Debug.Log("SIX");
    yield WaitForSecondsOrTap(5);
    Debug.Log("SEVEN");
    yield WaitForSecondsOrTap(5);
    Debug.Log("EIGHT");
    yield WaitForSecondsOrTap(5);
    }
function buttonPressed()
    {
    tappedWaitForSecondsOrTap();
    }

I deleted the other solutions as they are not what you’re after.

The way I tend to do it is, pretty much like you’ve figured, create my own yield subroutine using conditions:

function Start()
{
    WaitForSecondsOrKeyPress(3.14f,KeyCode.Escape);        //do not wait for it
    yield WaitForSecondsOrKeyPress(3.14f,KeyCode.Escape);  //wait for it
}

//coroutines can NOT have 'ref' or 'out' parameters (that'd be too good, I tried)
function WaitForSecondsOrKeyPress(delay:float, key:KeyCode):IEnumerator
{
    StartTheAction();

    while(!Input.GetKeyDown(key) && (delay > 0)) //check time and listen for keypress
    {
        delay -= Time.deltaTime;  //deduce time passed this frame.
        yield;                    //yield for one(1) frame.
    }

    StopTheAction();

    //return 3.14f; //coroutines can NOT return values like regular methods
}

function StartTheAction():void{}
function StopTheAction():void{}

EDIT: The example I had provided was functional but not logical. It now actually demonstrate how to wait for a delay or until the user press a key. This is not a one-fit-all solution but it could inspire someone to build his own.