Coroutine not working

I tried to make my LoadGameOverScene() a coroutine and called it when player dies. I used a print statement to check weather coroutine is being called or not and i found that only the statement written above yield return are working, below ones are not, becaz of which my GameOver scene is not loading :frowning:
Here’s my coroutine in Levels script-

 public IEnumerator LoadGameOverScene()
    {
        print("Couritine started"); //just to check
        yield return new WaitForSeconds(timeGap);
        SceneManager.LoadScene("Game Over");
    }

And my Player script-

 // player's death
        if (health <= 0)
        {
            Destroy(gameObject);
            AudioSource.PlayClipAtPoint(playerDeathSFX,
                gameCamera.transform.position, playerDeathVolume);

            StartCoroutine(FindObjectOfType<Levels>().LoadGameOverScene()); //is it correct to write like this?
        }

Plz help with it!

Hi Abhinav,

I think the problem is caused by Destroy(gameObject);. Coroutines on destroyed game objects get destroyed along with the game object. Instead of destroying the game object, disable the collider and disable the Renderer but not the game object.

Did this help?


See also:

1 Like

I didn’t got one thing…why in the lecture we started a coroutine inside LoadGameOverScene() and didn’t converted it into a coroutine also if the game object get deleted and anything being called inside it also deletes, then why the LoadGameOverScene() get finished properly, shouldn’t it also stop the next frame?? :woozy_face:

The StartCoroutine method returns a Coroutine object. It does not convert any method.

Is this the code you are referring to? If so, what do you mean by “next frame”? The WaitAndLoad method causes a delay, then loads the “Game Over” scene after x seconds.

Maybe I’m misunderstanding your question?

Let me make it simple
My doubt was when my player’s health is less than 0, It starts the coroutine LoadGameOverScene()(according to my script), but becaz of Destroy(gameobject) player’s gameobject get destroyed and my coroutine is not able to run the piece of code wriiten after yield return new WaitForSeconds(timegap) (this is what i understood till now).
So I want to know that in the lecture we made a coroutine inside LoadGameOverScene() and called the method and it works all fine. why?? Just as above shouldn’t it also stop once my gameObject gets destroyed?

We don’t destroy the Level component or the game object to which it is attached via Destroy. That’s why the coroutine is able to execute its entire code block. As long as the game object and the Level component exist and as long as they are active in the scene, the coroutine is able to run.

The Level component must not be attached to the Player game object.

Which version of Unity do you use, by the way? Maybe something changed in Unity.

Okay got it :+1:
( btw I’m using 2020.2.2f1 version)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms