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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Bug Stopping play mode by pressing play button or by changing a script have different outcomes

Discussion in 'Editor & General Support' started by marcospgp, Sep 19, 2022.

  1. marcospgp


    Jun 11, 2018
    I am running a long async task.

    If I stop play mode by making a change in code, the async task keeps running while its containing object is destroyed, causing a missing reference exception to be thrown when I try to reference

    Sadly, this is causing an object to be added to the scene when exiting play mode, so it can have destructive consequences that expand beyond the scope of a play session.

    This does not happen if I stop play mode by pressing the play button.

    This seems related to, which is locked.

    It seems that Unity should exit play mode before attempting a domain reload. Why is this not the case if play mode will be exited regardless?

    Perhaps async tasks were not something taken into account and are the only scenario where this makes a difference? I find that unlikely but possible.

    Edit: I noticed that exiting play mode by changing code, triggering a domain reload, only triggers an EnteredEditMode event and not ExitingPlayMode. Exiting play mode by pressing the play button triggers both.

    Edit2: Interestingly, if the code change that triggers the domain reload (and play mode exit) introduces an exception, both exceptions will be logged:


    But if the change does not introduce an exception, nothing will be logged, and the highlighted exception will be swallowed.

    Edit3: Exiting play mode by clicking play button, OnDestroy gets called after ExitingPlayMode:


    But when exiting play mode by making a code change, OnDestroy gets called out of the blue with no ExitingPlayMode. With no such event, I can't know that tasks running on a separate thread should be cancelled.

    This is my current implementation of an async task wrapper:
    Last edited: Sep 20, 2022