- Home /
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;
}
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.
I have already tried this and it is not this issue
If it helps the coroutine is called from another coroutine.
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.
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.
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.
Your answer
Follow this Question
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