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
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.