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

Coroutine not executing yield WaitForSeconds or anything after it

Discussion in 'Scripting' started by ela, Jan 10, 2017.

  1. ela

    ela

    Joined:
    Oct 12, 2016
    Posts:
    3
    I know this is not exactly a new question, but I've been searching the internet all week for an answer. The code works fine, except the yield WaitForSeconds never executes. I've posted my code as well as all my Debug.Log checks that appear in the console. For the life of me I cannot figure out why it's stepping into the coroutine, but not executing the yield.I've tried to move things around, but nothing seems to work.

    What I'm trying to achieve in this code:

    The game object (in this case the stone) is destroyed when the player clicks on it and after a certain amount of time the stone will re-spawn at the same location. This script will need to work for several different game objects.
    help.png
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Try calling the Destroy function after the yield WaitForSeconds.
     
    Kurt-Dekker likes this.
  3. ela

    ela

    Joined:
    Oct 12, 2016
    Posts:
    3
    but I want it to wait to re-spawn (instantiate) not destroy?
     
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    When you destroy an object, all its coroutines are stopped.
    If you want to destroy an object, wait, then res-spawn a new one. You have to write the coroutine doing that into another object, for instance a "CollectibleItemManager".
     
    Last edited: Jan 10, 2017
    ela likes this.
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    949
    You can not expect the coroutine to carry on when the object is destroyed!.
    I guess you should spawn the new object first, then destroy the old one.
    Then put your yield WaitForSeconds into the spawned object. You can get the delay by disabling the object's renderer (and collider) and reenabling it after yield WaitForSeconds finishes.
     
    ela likes this.
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Just break it down and it looks so obvious.

    *When the user presses the mouse on this object*
    - Log a message
    - Destroy the object that this script is attached to
    - Start a couroutine (Oh wait, we don't exist anymore, how does that work?, It doesn't!)
     
  7. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    949
    possibly looks like pulling our legs...
     
  8. ritesh_khokhani

    ritesh_khokhani

    Joined:
    Jun 26, 2013
    Posts:
    47
    You should probably create another script (like SpawnManager.cs or PoolManager.cs) which manages all spawning and destroying the objects, this will also reduce complexity while developing..
     
    ela likes this.
  9. ela

    ela

    Joined:
    Oct 12, 2016
    Posts:
    3
    Thanks everyone, this was a great help. I guess I assumed that the coroutine would still execute since it stepped into it, but not finished it, after the object was destroyed.
     
  10. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    Destroy() is a bit tricky about that. It doesn't actually fully destroy the object immediately, but marks it as destroyed. As the docs say: "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering." It's also the reason why, when making editor scripts, DestroyImmediate() is available as an alternative for cases when it truly is needed.
     
    Ryiah and ela like this.