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 yourulenl · Sep 04, 2014 at 12:35 PM · coroutineforeachwhile

C# while within foreach doesn't complete

Hi,

I'm iterating through objects within a sphere (Physics.OverlapSphere). There are two types of objects found (parent and child). If a parent is found a while loop is triggered to find the distance to it's child objects. These distances needs to be concatenated into a string. (for network optimization). The problem is that the values are not concetenated but spit out separately.

Expected Debug.Log() output:

/ 0 / 50 / 1 / 48.5 / 2 / 11 / 3 / 15

Result

/ 0 / 50

/ 1 / 48.5

/ 2 / 11

/ 3 / 15

I already placed the while loop in a coroutine, but with no result. I stripped the code down to a bare minimum. I'd appreciate a fresh look on this!

 using UnityEngine;
 using System.Collections;
 
 public class tester : MonoBehaviour {
 
     private float radius = 100.0F;
     private int nrOfObects = 5;
     private int objectCounter = 0;
 
     public void FixedUpdate() {
 
         foreach (Collider collider in Physics.OverlapSphere(transform.position, radius)) {
 
             StartCoroutine( combineValues( collider ) );
         }
     }
 
     IEnumerator combineValues( Collider collider ){
 
         string combinedValues = "";
 
         while (objectCounter < nrOfObects) {
 
             if (collider.name == this.name + "_child_" + objectCounter) {
 
                 float distance = Vector3.Distance(transform.position, collider.transform.position);
             
                 combinedValues += " / "+objectCounter.ToString()+" / "+distance.ToString();
 
                 objectCounter++;
             }
         }
 
         Debug.Log (combinedValues);
         yield return null;
     }
 }
Comment
Add comment · Show 2
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
avatar image GameVortex · Sep 04, 2014 at 12:44 PM 0
Share

You have an if statement inside your while loop, if it does not evaluate to true it will never increment the objectCounter value which means it will continue to be false because nothing has changed and the while loop will not end.

avatar image yourulenl · Sep 04, 2014 at 01:11 PM 0
Share

Thank you for your reply, The if evaluating false is not the case, the objects are found and the counter is incrementing as you can see in the "expected debug.log() output". $$anonymous$$y problem is that the Debug.Log() is triggered before the while loop is done

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by supericecream · Sep 04, 2014 at 01:33 PM

Probably because that the code IS in the coroutine. Put it in a normal function and return the finished string, then Debug.Log the returned value.

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
avatar image
0

Answer by yourulenl · Sep 04, 2014 at 01:53 PM

Found it. the string combinedValues = ""; should be above the initialization of the foreach colider loop. In this case it gets reset on each found colider

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with while loop in Coroutine 2 Answers

c# Using an IEnumerator yield WaitForSeconds to temporarily pause a While loop 3 Answers

StartCoroutine for each object in a foreach() Loop 1 Answer

Coroutine stops when changing bool 1 Answer

Waiting for a mouse click in a Coroutine 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