Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coroutine Issues Despite Watching videos, rvwing how coroutines are used.

Discussion in 'Scripting' started by adiboy360, Nov 27, 2020.

  1. adiboy360

    adiboy360

    Joined:
    Dec 3, 2019
    Posts:
    5
    EDIT: FIXED. I was destroying the gameObject too soon, terminating the coroutine. I destroyed the game object after the routine and it worked fine.

    I've used coroutines in two previous projects with no issues. Here, on such a simple matter, it's just not clicking.

    Goal: Load game over scene 1 second after player is destroyed.

    Relevant code:

    (In Player_Controls.cs script: )

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         Destroy(gameObject);
    4.         GameObject explosion = Instantiate(deathExplosion, transform.position, transform.rotation);
    5.         Destroy(explosion, explosionDuration);
    6.         FindObjectOfType<Scene_Manager>().LoadLostScene();
    7.     }
    (In Scene_Manager.cs script: )

    Code (CSharp):
    1. public void LoadLostScene()
    2.     {
    3.         StartCoroutine(WaitAndLoad());
    4.     }
    5.  
    6.     IEnumerator WaitAndLoad()
    7.     {
    8.         yield return new WaitForSeconds(1f);
    9.         SceneManager.LoadScene("GAME_OVER_YOU_LOST");
    10.     }
    ---

    So, it's just not clear to me where I'm messing up. I'm destroying the object, starting particle effects, calling the LoadLostScene() function from Scene_Manager.cs, which runs the coroutine. At least, I think I'm doing those things in that order. Any help would be greatly appreciated.
     
    Last edited: Nov 27, 2020
  2. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    What the error/issue actually. What's happening?
     
  3. adiboy360

    adiboy360

    Joined:
    Dec 3, 2019
    Posts:
    5
    @IcePhoenix_0101 Thank you for offering to help. The issue is that the coroutine was not working. I figured out why though. Just had to destroy the game object towards the end, after the routine is complete.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Other GameObjects can be used to "run" your coroutines for longer periods of time, if appropriate. This pattern is often called a "coroutine runner" and is usually implemented with a singleton-ish GameObject that hangs around running your coroutine. That lets you drive a single coroutine through multiple scene loads/unloads, assuming you mark the coroutine runner GameObject as DontDestroyOnLoad().
     
    adiboy360 likes this.
  5. adiboy360

    adiboy360

    Joined:
    Dec 3, 2019
    Posts:
    5
    @Kurt-Dekker Thank you! I didn't know that was a thing, but I believe that's what I eventually ended up doing. I used another script and OnTriggerEnter2D to destroy the desired gameObject. I appreciate the help :)
     
    Kurt-Dekker likes this.