Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. The Burst compiler has its own forum section now.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Script continuing to execute when Unity is PAUSED.

Discussion in 'Scripting' started by rayD8, Apr 2, 2020.

  1. rayD8

    rayD8

    Joined:
    Mar 7, 2018
    Posts:
    24
    I'm testing an SDK that interfaces with some hardware.
    I can connect connect to things fine. When I dispose of the resources (via a key press) and then stop playing... all seem fine - I can even edit/save the scene... However, the next play attempt locks up Unity. If I attempt to exit Unity instead of play the scene again, it will also lock up and crash. Both happen 100% of the time.

    So in an effort to debug it... I added some debug.log lines.
    Even when the Unity editor is paused - an EventHandler connected to the SDK continues to fire off an event every second or so - as it normally does when actually playing/running.
    Anyone know if that's "normal" behavior for things being paused in the editor??

    I've tried unsubscribing to the EventHandler, and then having the SDK part it's connect to disposed. That works fine. But the crashing behavior is still exactly the same. I've create a build, and the build works, but crashes on exit too.

    I never get any error messages, and the Unity profiler hasn't been helpful for this since things run fine until stopping/exiting. Any ideas what might be happening?
    (the script with the EventHandler is not an editor script or inheriting from monobehavior)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    12,327
    As long as you are using the .OnEnable() / .OnDisable() callbacks to subscribe / unsubscribe in Monobehaviors, it should be fine.

    You cannot rely on those two events in ScriptableObjects: ScriptableObject lifecycles are fundamentally different than MBs.

    Attach the debugger and put breakpoints on the subscribe and unsubscribe, make sure they match. If they do then the crash is something else.

    If the internal SDK is attaching something and failing to let it go, check if there's some other deinit() style call you have to make. It might be lacking such a thing if they assume you'd only ever run it as a standalone process.
     
  3. rayD8

    rayD8

    Joined:
    Mar 7, 2018
    Posts:
    24
    Wow. Thank you. Your reply was so fast it went through before I added a line clarifying it's not using monobehavior.

    I'm using a "hotkey" so I can manually tell it to unsubscribe... and then tried waiting seconds before stopping. Also, a class that interfaces with the SDK can't inherit from Monobehavior, or Unity crashes without it even playing. I'll try passing/getting a reference from that class to a class that uses Monobehavior, and see if I can set up an EventHandler there - instead of the "regular" C# (it doesn't inherit anything).

    So a class using UnityEngine namespace to write to the debug window, but not using Monobehaviors, can continue to write info to the window when Unity is paused? (trying to be sure I understand that part)
     
    Last edited: Apr 2, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    12,327
    There are ways to continue getting callbacks after Unity has stopped running and is paused. Generally you don't want that unless you know what you want. If the SDK ever does that, it might explain what you see.

    The usual pattern is the OnEnable/OnDisable to sub/unsub. If you want otherwise to use a key for on/off, I recommend another boolean to simply control behavior of the subscribed handler, such that you can preserve the clean OnEnable/OnDisable boundary.

    Generally you don't want to call anything in Unity from other threads. Most such things will crash. In practice, calling Debug.Log() has been widely reported to work, but the docs do not promise it will work, and certainly don't promise it will continue to work. Use it at your peril.
     
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,244
    Try switch the profiling target from Editor to Playmode (or toggle on "Profile Editor") to profile the editor and not stop profiling on pause/exiting play mode.
     
unityunity