Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AlexGan001 · Sep 10, 2016 at 07:52 AM · coroutinenot workingfor-loopyield waitforseconds

Why is unity exiting a loop inside a coroutine after its first run through?

Hello, I need help to figure out what is wrong with my code. I run this coroutine and give it a number of lines to loop through (more than one) (I have checked that this number isn't 1), but the loop only runs once and stops at the end (possibly something to do with the WaitForSeconds command). Please tell me why it doesn't continue with it's loop and help me with a solution.

 private IEnumerator If (List<string> ifLines) {
             for (int i = 0; i < ifLines.Count; i++) {
                 int wait = 0;
                 string line = ifLines [i];
                 if (line.Contains ("opendoor")) {
                     if (line.Length == 11) {
                         char[] chrs = line.ToCharArray();
                         if (chrs[8] == '(' && chrs[10] == ')') {
                             string sChr = "" + chrs[9];
                             int chr = 0;
                             int.TryParse (sChr, out chr);
                             if (chr > 0 && chr < 10) {
                                 Debug.Log ("Opened Door " + chr);
                             }
                         }
                     }
                 } else if (line.Contains ("closedoor")) {
                     if (line.Length == 12) {
                         char[] chrs = line.ToCharArray();
                         if (chrs[9] == '(' && chrs[11] == ')') {
                             string sChr = "" + chrs[10];
                             int chr = 0;
                             int.TryParse (sChr, out chr);
                             if (chr > 0 && chr < 10) {
                                 Debug.Log ("Closed Door " + chr);
                             }
                         }
                     }
                 } else if (line.Contains ("lights")) {
                     if (line.Length == 12) {
                         char[] chrs = line.ToCharArray();
                         if (chrs[6] == '(' && chrs[8] == ')' && chrs[9] == '.' && chrs[10] == 'o' && chrs[11] == 'n') {
                             string sChr = "" + chrs[7];
                             int chr = 0;
                             int.TryParse (sChr, out chr);
                             if (chr > 0 && chr < 10) {
                                 Debug.Log ("Light " + chr + " activated");
                             }
                         }
                     } else if (line.Length == 13) {
                         char[] chrs = line.ToCharArray();
                         if (chrs[6] == '(' && chrs[8] == ')' && chrs[9] == '.' && chrs[10] == 'o' && chrs[11] == 'f' && chrs[12] == 'f') {
                             string sChr = "" + chrs[7];
                             int chr = 0;
                             int.TryParse (sChr, out chr);
                             if (chr > 0 && chr < 10) {
                                 Debug.Log ("Light " + chr + " deactivated");
                             }
                         }
                     }
                 } else if (line.Contains ("wait")) {
                     if (line.Length == 7) {
                         char[] chrs = line.ToCharArray();
                         if (chrs[4] == '(' && chrs[6] == ')') {
                             string sChr = "" + chrs[5];
                             int chr = 0;
                             int.TryParse (sChr, out chr);
                             if (chr > 0 && chr < 10) {
                                 wait = chr;
                             }
                         }
                     }
                 }
                 yield return new WaitForSeconds (wait);
     
             }
             //yield return null;
         }

Comment
Add comment · Show 6
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 Sergio7888 · Sep 10, 2016 at 08:44 AM 0
Share

i have made a simple test her and it execute the cor correct try put a Debug.Log(ifLines.Count); after the first line of the for to check the count.

avatar image AlexGan001 Sergio7888 · Sep 10, 2016 at 08:56 AM 0
Share

I have already tried this and it is not this issue

avatar image AlexGan001 AlexGan001 · Sep 10, 2016 at 08:58 AM 0
Share

If it helps the coroutine is called from another coroutine.

Show more comments
avatar image AlexGan001 Sergio7888 · Sep 10, 2016 at 09:06 AM 0
Share

No, I am not changing the timescale. I had no problem with this before I changed the if statements inside the loop, but don't understand how they could affect this.

avatar image Sergio7888 AlexGan001 · Sep 10, 2016 at 09:36 AM 0
Share

I tried here with various string and has no error in this code its execute and wait and continue execution, you said its running inside another coroutine, maybe the problem is there or you are calling "StopAllCoroutines" somewhere in this behaviour.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AlexGan001 · Sep 10, 2016 at 12:31 PM

Problem solved. This issue was that I hadn't realised that Lists reference to each other, so when I cleared the ifList, ifLines went blank aswell.

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

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

Related Questions

Run coroutine X amount of times in Y seconds? 3 Answers

yield return new WaitForSeconds not working 1 Answer

StopCoroutine with IEnumerator not working 2 Answers

Yield Never Works 4 Answers

WaitForSeconds is breaking out of my IEnumerator 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