Access Coroutine from another script

Hi all,

So I’ve been wondering why this piece of code won’t work:

private void Die()
    {
        StartCoroutine(FindObjectOfType<Level>().LoadGameOver());
    }

Where in Level it looks like this:

public IEnumerator LoadGameOver()
    {
        yield return new WaitForSeconds(2);
        SceneManager.LoadScene("Game Over");
    }

Thanks in advance,

Marcell

Hi,

What do you mean by “won’t work”? Have you tested it? If so, what was the result? And what did you do with the object to which the Die method belongs? If you destroy that object, comment out the Destroy method to see what happens. Maybe the Coroutine object which gets generated by StartCoroutine is associated with the “destroyed” game object.