Search Unity

OnApplicationPause not being called when MonoBehaviour added at Runtime

Discussion in 'Scripting' started by doarp, Feb 28, 2020.

  1. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    I create a GameObject and attach a MonoBehaviour with OnApplicationPause method in it, I do this in a static method with [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] Attribute on it.
    The MonoBehaviour works, but OnApplicationPause event isn't being called.
    If I add the same MonoBehaviour to an existing GameObject instead in the Editor, everything works just fine.

    It this a known issue? Am I doing anything wrong?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What platform are you testing this on?
     
  3. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    iPhone 7 Plus
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I know Apple has changed some things. I've been having to remove the behavior in background part from the info.plist file. But maybe this post has more info.
    https://forum.unity.com/threads/onapplicationpause-not-being-called-in-ios-build.455426/

    Either way, there is no guarantee you'll get the same behavior in the editor as you would on the device. I would test just having a script already in the scene with the OnApplicationPause method on it and see if that runs.

    Use something like LogViewer on the asset store to see your console on the final build, will help you with debugging.

    If the behavior is the same with the script already in the scene, then it may be an iOS thing or it may be related to something in the other post about custom background behavior. If it's not, it could possibly be a bug.
     
  5. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    I was probably unclear. All tests are done on the device.
    It works when the script is part of a game object in the scene. It doesn’t work if I create the game object at runtime and add the script component to it.