- Home /
Question by
robert97mc · Aug 25, 2021 at 05:39 PM ·
c#unity 5arraystartcoroutine
How to StartCoroutine on all array elements?
So I have array of 4 objects and I want to start coroutine on all of them but for some reason coroutine starts only on first object. Here is code:
public void SimulateMatches()
{
int player1Score = 0;
int player2Score = 0;
int maxPoints = GameManager.maxRoundPoints;
StartCoroutine(SimulateMatchCoroutine(player1Score, player2Score, maxPoints));
}
public IEnumerator SimulateMatchCoroutine(int score1, int score2, int maxPoints)
{
foreach (TextMeshProUGUI score in scores)
{
while (score1 < maxPoints && score2 < maxPoints)
{
yield return new WaitForSeconds(2);
int chance = Random.Range(1, 3);
if (chance == 1)
score1++;
else if (chance == 2)
score2++;
score.text = score1 + " - " + score2;
}
}
}
Comment
Best Answer
Answer by imercayli · Aug 25, 2021 at 06:25 PM
Because score1 and score2 independent from score array. Condition on while is completed in first array element. So its not going inside while again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613042147im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get an array of children from an existing array 1 Answer
Array foreach problem 1 Answer
Making a compass point towards the closest enemy 4 Answers
Store multiple sprite in code C# 0 Answers
Startcoroutine not working second time 2 Answers