Im fairly new to unity and coding in general, can someone tell my why this just repeats forever

void BossAttack1()
{
for (int i = 0; i < 5; i++)
{
if (boolattack1)
{
StartCoroutine(RepeatPhase1());
boolattack1 = false;
}

    }
}

IEnumerator RepeatPhase1()
{
    yield return new WaitForSeconds(5);
    Attack1();
    boolattack1 = true;
    StopCoroutine(RepeatPhase1());
}

BossAttack1() is in the update function.

Because if its calling BossAttack in update which is called every frame

  • It enters your for loop every frame 5 times, which calls your CoRoutine if boolattack = true
  • In 5 seconds inside your coroutine you set boolattack back to true
  • Your if statement is then met on the next Update and it calls Startcoroutine again.

Also you don’t need to call StopCoroutine at the end of your coroutine