- Home /
Using yield to pause a function and start another one.
So I've been trying to understand how yield works and whenever I believe I do my code just doesn't work...
I have a GoTo action and if the character is sitting down I want him to stand up. This is my code for the GoTo ()
function GoToAction () {
//Add StandUp (if player is sitting)
if (currentState == PlayerStates.SittingDown || previousState == PlayerStates.SittingDown){
yield StandUp();
}
var selectedPosition : Vector3;
if (currentAction.selectedDestination == null){
selectedPosition = currentAction.selectedPosition; //Used for going to a FLOOR position.
}
else {
selectedPosition = currentAction.selectedDestination.position;
}
GetComponent(NavMeshAgent).SetDestination(selectedPosition);
currentState = PlayerStates.GoingTo;
}
and here is the StandUp () that I would like to run if the player is sitting:
function StandUp(): IEnumerator {
if (playerProperties.isSitting){
playerProperties.isSitting = false; // trigger getUp anim
}
if (playerProperties.isSittingTable){
playerProperties.isSittingTable = false; // trigger getUp anim
currentAction.selectedObject.GetComponent(Obj_Properties_Class).sitProperties.animObj.GetComponent(Animator).SetBool("sitDown", false); //trigger standUp object anim
}
if (playerProperties.isLaying){
playerProperties.isLaying = false; // trigger getUp anim
}
//closestTrigger
currentSitTrigger.parent.GetComponent(Obj_Properties_Class).sitProperties.availableSitsArray.Add(currentSitTrigger);
currentSitTrigger = null;
currentState = PlayerStates.StandingUp;
if (animController.currentBaseState.nameHash != animController.idleState){
yield; //I would assume this holds this function until complete. Once terminated it goes back to the original GoTo()?
}
currentState = PlayerStates.Idle;
GetComponent(NavMeshAgent).enabled = true;
}
Right now nothing happens...whenever I have the yield on the GoToAction the function doesn't do anything, even if I add a Print before it... I just can't figure out how to do this!
Any help very appreciated.
Answer by Ray-Pendergraph · Jun 29, 2013 at 08:23 PM
I don't write UnityScript so I forget what syntactic sugar it gives you. I am pretty sure the problem is on line 4 because you cannot just yield up the IEnumerator because Unity does not care what's yielded up (unless it's a YieldInstruction I think). You need to yield and start another Coroutine to make one call another. In C# it looks like this:
protected IEnumerator OverallProcess ()
{
yield return StartCoroutine (SubProcess1 ());
Debug.Log("In between");
yield return StartCoroutine (SubProcess2 ());
}
protected IEnumerator SubProcess1 ()
{
Debug.Log ("Process one A");
yield return 0;
Debug.Log ("Process one B");
yield break;
}
protected IEnumerator SubProcess2 ()
{
Debug.Log ("Process two");
yield return 0;
}
Also I think the yield on line 21 will only happen once. Your comment suggests that you want it to keep yielding until the condition is true so you will need to change the 'if' to 'while'.