0
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ I'm unable to reproduce the problem with the code you've shown here. Putting this code into a new Unity project does not cause a freeze. Please edit your question to include a Minimal Complete Verifiable Example: every line of code we'd need to put into a new, empty project to successfully reproduce the issue. \$\endgroup\$ Commented May 31, 2022 at 13:55
  • \$\begingroup\$ @DMGregory Thank you for trying to help :) I will edit the question \$\endgroup\$ Commented May 31, 2022 at 13:59
  • 2
    \$\begingroup\$ As I said, you need to share every line of code. We should be able to paste the example unmodified, without making up the contents of the ChangeState() method or the declaration of the faintSecs variable or Monster class. \$\endgroup\$ Commented May 31, 2022 at 14:07
  • \$\begingroup\$ I suspect the issue lies in the changestate function. 90% of the time I have endless loops, it's because I have a property that returns itself somewhere. For example public float Health => Health; instead of public float Health => health; maybe take a look for that. \$\endgroup\$ Commented Jun 1, 2022 at 6:17
  • \$\begingroup\$ A couple of side notes that won't fix your freeze: You are setting the faintSecs value but not using it, and it's not necessary to yield break; at the end of a coroutine, only if you want to exit the coroutine in the middle. \$\endgroup\$ Commented Jun 1, 2022 at 18:56

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.