Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with coroutines in 2D game C#

Discussion in 'Scripting' started by Shippety, Apr 14, 2015.

  1. Shippety

    Shippety

    Joined:
    May 9, 2014
    Posts:
    31
    I am using as coroutine so that something happens 2 seconds after another few things.
    Code (CSharp):
    1. IEnumerator MyMethod()
    2. {
    3. Debug.Log ("Dead");
    4. Destroy (PlayerSprite);
    5. yield return new WaitForSeconds(2.0f);
    6. Application.Loadlevel("Level 1")
    7. Debug.Log("Loading level?");
    8. }
    Want I want this to do is send 'dead' and such, wait 2 seconds, and then reload the level with Application.LoadLevel. I call the coroutine later like this:

    Code (CSharp):
    1. StartCoroutine(MyMethod());
    The Debug.Log("Dead"); is sent and everything before, but nothing after the 2 seconds happens, including the second Debug.Log! Anyone know what part I'm phrasing incorrectly? - JS
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    I would assume because it's loading a new level, all the code is stopping completely.
     
    Dizzy-Games likes this.
  3. Dizzy-Games

    Dizzy-Games

    Joined:
    Apr 7, 2015
    Posts:
    15
    this is mostly like the situation here, move the "Loading Level" log above Application.Loadlevel
     
    Shippety likes this.
  4. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    I would be surprised if this is the case, Since there is no yield statement after the call to LoadLevel and I don't think unity would hijack execution from you without any yield specified.

    I would more bet that the script or or the gameObject the script is on is PlayerSprite which you destroy prior to the wait.

    I'm guessing no level load occurs?
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    As bdev says, if you destroy an object that a coroutine is running on, it also deletes the coroutine and it will never finish.
     
    Shippety likes this.
  6. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    probably important to note that unity won't 'stop' the coroutine immediately with the call to delete ( as it'll never just eject your code ), rather if you yield return after the object was destroyed within the coroutine, control will not be returned to the coroutine.
     
  7. Shippety

    Shippety

    Joined:
    May 9, 2014
    Posts:
    31
    The answer, as it turns out, was that part of the coroutine involved destroying the player, the gameobject the script itself was attached to. Once it was destroyed, the coroutine couldn't continue... thanks for all of your help!
     
    Dizzy-Games likes this.