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. Dismiss Notice

Question Disabling an object with a coroutine running on it

Discussion in 'Scripting' started by karderos, Jul 5, 2023.

  1. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    I know that if an object is setactive to false, all coroutines running on scripts attached to that object are stopped

    my question is, will they resume when the object is set back to active?

    Is it a good practice for example if I have objects in a pool, to take care to stop all coroutines before processing them to the pool?

    Or can I rest assured that if I just disable the object I wont have more trouble with any coroutines that were running on it, and they wont start running again when I reuse the object when setting it back to active?

    Thanks
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Kurt-Dekker likes this.
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    This is indeed true, but sometimes it's nice to get confirmation, from those more experienced, that what you think may be true is in fact the case.

    Just saying, because in the past, I've read documentation and written test code that leads me to believe one thing, only to have my misunderstanding pointed out later on.

    I realise the opening post may have seemed about a trivial matter, but triviality is relative.

    And the answer to the OP is yes, the coroutine will stop and won't automatically restart when the game object is re-enabled.
     
    Chubzdoomer and karderos like this.