Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by castor · Jun 29, 2013 at 04:03 PM · yieldreturnwait

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Yield until color has been chosen, return color (or Color index) 1 Answer

WaitForSeconds not running 2 Answers

Have a function wait unitl a boolean is false 1 Answer

yield while animation plays in mecanim 1 Answer

WaitUntil Combined With sqrMagnitude is not working? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges