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 darthbator · Mar 12, 2013 at 01:09 AM · errorcoroutinecoroutinesstuck

Issue with nested coroutines getting stuck

So I'm having an issue yielding coRoutines from coRoutines in my game. First just a touch of background. In my game after a player has scored a point both players then return to their "starting" location, then the puck is inbounded and gameplay starts again. I have a few coRoutines all chaining into one another to get this to work.

When you score a goal is sets off a coRoutine in the gameManager that will update your score, that part works great.

 public IEnumerator scoreUpdate (string scoringPlayer, int scoreValue) {
         points[scoringPlayer] += scoreValue;
         if (points[scoringPlayer] >= scoreMax) {
             rounds[scoringPlayer]++;
             turnRound();
         }
         yield return StartCoroutine(resetPlayerPos());
         Debug.Log("out of Co");
         foreach (KeyValuePair<string, GameObject> player in players) {
             if (player.Value.name != scoringPlayer) {
                 puckToPlayer(player.Value.transform);
             }
         }
     }

After the score is updated I call yield to a coRoutine that should run until all players get back to their starting positions. I'm doing that as follows

 IEnumerator resetPlayerPos () {
         foreach (characterMotor motor in playerMotors.Values) {
             StartCoroutine(motor.moveToStart());
         }
         foreach (KeyValuePair<string, bool> playerPos in atStartPos) {
             while (playerPos.Value == false) {
                 yield return null;            
                 //Debug.Log(playerPos.Key + " " + playerPos.Value.ToString());
             } 
         }
     }

Which is in turn contacting the following coroutine on my player and telling him to move back to his start point.

 public IEnumerator moveToStart () {
         float progress = 0f;
         canMove = false;
         gameManager.Instance.atStartPos[gameObject.name] = false;    
         Vector3 initPos = transform.position;
         while (progress < 1) {
             transform.position = Vector3.Lerp(initPos, playerStartPos, progress);
             progress += Time.deltaTime * resetPosSpeed;
             yield return null;
         }
         gameManager.Instance.atStartPos[gameObject.name] = true;
         canMove = true;
         Debug.Log(gameObject.name + " move Complete!");
     }

Ok so now that I have bored almost everyone out of here with pasted code I can get to the exact issue. The character position reset seems to all be working fine. However the following section of the resetPlayer method in my gameManager never seems to exit.

 foreach (KeyValuePair<string, bool> playerPos in atStartPos) {
                 while (playerPos.Value == false) {
                     yield return null;            
                     //Debug.Log(playerPos.Key + " " + playerPos.Value.ToString());
                 } 

Which is odd because the coRoutine on the actual player itself should change that value here

 gameManager.Instance.atStartPos[gameObject.name] = true;

I'm SUPER confused as to why this isn't working... And why that coRoutine gets stick

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
Best Answer

Answer by darthbator · Mar 13, 2013 at 12:59 AM

Just to sort of close the loop on this one the issues was that I was checking for a value change in the KeyValuePair that I make in the loop and not in the class level Dictionary. I changed it to the code below and now it works awesome!

 List<string> playerPosStrings = new List<string>(atStartPos.Keys);
         foreach (string entry in playerPosStrings) {
             while (atStartPos[entry] == false) {
                 yield return null;            
             } 
         }
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

10 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

Related Questions

Example of co-routine for documentation 1 Answer

While loop out of control...maybe? I'm not sure. Trying to track Coroutines progress. 1 Answer

Coroutine Won't Stop Using IEnumerator 1 Answer

Bullet Script 2 Answers

WebGL loader stuck at 90% 0 Answers


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