Skip to main content
added 922 characters in body
Source Link
UserUser
  • 171
  • 1
  • 11

Every time I call this function that starts the coroutine Faint, it freezes the game. I do not understand why this freezes the game though, because the coroutine does not have any loop.

public void StartFaint()
{
    faintSecs = 1.5f;
    StartCoroutine("Faint");
}

IEnumerator Faint()
{
    ChangeState(true);
    yield return new WaitForSeconds(1.5f);
    ChangeState(true);
    yield break;
}

ChangeState() just changes the value of some variables. The Coroutine is just being started once.

The above scripts are a part of a script named Monster. Another script accesses this script and calls the above function like this:

IEnumerator SpecialAttackLaunch()
{
    Debug.Log("special attack launch");
    ani.SetTrigger("specialAttack");
    
    List<GameObject> enemiesHit = GetEnemiesInRange(transform);
    yield return new WaitForSeconds(0.24f);

    foreach(GameObject enemy in enemiesHit)
    {
        Debug.Log("hi");
        enemy.GetComponent<Monster>().ChangeHealth(-1 * attackAmount);
        enemy.GetComponent<Monster>().StartFaint(); //works fine when only this line deleted
    }

    yield break;
}

The code works perfectly fine if I just delete the

enemy.GetComponent<Monster>().StartFaint();

so I was assuming the problem was with the former coroutine and not the latter one.

According to the internet, yield break is supposed to terminate the coroutine...but maybe I'm mistaken?

Thank you in advance.

Every time I call this function that starts the coroutine Faint, it freezes the game. I do not understand why this freezes the game though, because the coroutine does not have any loop.

public void StartFaint()
{
    faintSecs = 1.5f;
    StartCoroutine("Faint");
}

IEnumerator Faint()
{
    ChangeState(true);
    yield return new WaitForSeconds(1.5f);
    ChangeState(true);
    yield break;
}

ChangeState() just changes the value of some variables. The Coroutine is just being started once.

According to the internet, yield break is supposed to terminate the coroutine...but maybe I'm mistaken?

Thank you in advance.

Every time I call this function that starts the coroutine Faint, it freezes the game. I do not understand why this freezes the game though, because the coroutine does not have any loop.

public void StartFaint()
{
    faintSecs = 1.5f;
    StartCoroutine("Faint");
}

IEnumerator Faint()
{
    ChangeState(true);
    yield return new WaitForSeconds(1.5f);
    ChangeState(true);
    yield break;
}

ChangeState() just changes the value of some variables. The Coroutine is just being started once.

The above scripts are a part of a script named Monster. Another script accesses this script and calls the above function like this:

IEnumerator SpecialAttackLaunch()
{
    Debug.Log("special attack launch");
    ani.SetTrigger("specialAttack");
    
    List<GameObject> enemiesHit = GetEnemiesInRange(transform);
    yield return new WaitForSeconds(0.24f);

    foreach(GameObject enemy in enemiesHit)
    {
        Debug.Log("hi");
        enemy.GetComponent<Monster>().ChangeHealth(-1 * attackAmount);
        enemy.GetComponent<Monster>().StartFaint(); //works fine when only this line deleted
    }

    yield break;
}

The code works perfectly fine if I just delete the

enemy.GetComponent<Monster>().StartFaint();

so I was assuming the problem was with the former coroutine and not the latter one.

According to the internet, yield break is supposed to terminate the coroutine...but maybe I'm mistaken?

Thank you in advance.

Source Link
UserUser
  • 171
  • 1
  • 11

Unity Coroutine freezes game even though no loop

Every time I call this function that starts the coroutine Faint, it freezes the game. I do not understand why this freezes the game though, because the coroutine does not have any loop.

public void StartFaint()
{
    faintSecs = 1.5f;
    StartCoroutine("Faint");
}

IEnumerator Faint()
{
    ChangeState(true);
    yield return new WaitForSeconds(1.5f);
    ChangeState(true);
    yield break;
}

ChangeState() just changes the value of some variables. The Coroutine is just being started once.

According to the internet, yield break is supposed to terminate the coroutine...but maybe I'm mistaken?

Thank you in advance.