Search Unity

Initialization throw error when used in ScriptableObject

Discussion in 'Game Foundation' started by KEM4, Sep 6, 2020.

  1. KEM4

    KEM4

    Joined:
    Nov 22, 2017
    Posts:
    8
    Works at:
    If the following actions are performed within the game object everything works without an error.

    Scenario:

    Performing initialization within the ScriptableObject OnEnable method causes the following error when exiting the play mode. Error: https://prnt.sc/ucga7n (Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?))
    If the same script is created as MonoBehaviour and attached to the gameObject within the scene, the error does not appear.

    Issue:

    When working with a new game I do not tend to create everything within the game object but use ScriptableObjects to enable and enhance systems. This error does not allow me to do so. Another thing that happens when an error occurs exiting the play mode a game object is created within the scene https://prnt.sc/ucgac6 but this does not happen when used within the second method (gameObject + MonoBehaviour script)

    Expected result (on the user side):

    I expect that when the OnEnable method is called GameFoundation will be initialized without throwing errors both Editor and Play mode.

    Script
    https://prnt.sc/ucgafo

    Code (CSharp):
    1.  
    2.     [CreateAssetMenu(fileName = "New app settings", menuName = "ECB/AppSettings", order = 1)]
    3.     public class AppSettings : ScriptableObject
    4.     {
    5.         [SerializeField] private App app;
    6.  
    7.         private void OnEnable()
    8.         {
    9.             LocalPersistence storage = new LocalPersistence("playerData", new JsonDataSerializer());
    10.             PersistenceDataLayer dataLayer = new PersistenceDataLayer(storage);
    11.  
    12.             GameFoundation.Initialize(dataLayer, OnGameFoundationInitialized);
    13.         }
    14.  
    15.         private void OnDisable()
    16.         {
    17.             GameFoundation.Uninitialize();
    18.         }
    19.  
    20.         private void OnGameFoundationInitialized()
    21.         {
    22.             AppManager.Initialize();
    23.         }
    24.     }
    25.  
    Unity version: 2020.1.3f1

    Let me know if you need additional information in order to reproduce the issue.

    Question: is there a way to fix this besides using the MonoBehavour?

    Thanks
     
  2. KEM4

    KEM4

    Joined:
    Nov 22, 2017
    Posts:
    8
    Is this really not important to look into since this causes errors? Would appreciate some answer here.
     
  3. mingz-unity

    mingz-unity

    Unity Technologies

    Joined:
    Oct 12, 2017
    Posts:
    63
    Hi @KEM4 - I've forwarded this to the team to investigate. We'll get back to you soon with more information.
     
    erika_d likes this.
  4. mingz-unity

    mingz-unity

    Unity Technologies

    Joined:
    Oct 12, 2017
    Posts:
    63
    Hi @KEM4, the team looked at this issue you reported, and we think this behaviour is caused because OnEnable and OnDisable messages are called in the editor for ScriptableObject when the game isn't playing.

    This explains why the GameFoundationUpdater is visible in the scene and generates these errors, it is because it is created at editor time instead of runtime. It should work as expected in a real build.

    If they want to fix it for now, they can wrap their GameFoundation Initialization/Uninitialization in if (Application.isPlaying) conditions.

    Hope this helps. Let us know if you need further assistance on this.
     
    erika_d likes this.
  5. KEM4

    KEM4

    Joined:
    Nov 22, 2017
    Posts:
    8
    Been off for a while, so I apologize for the late replay.

    Well, in some way it does make sense and I will do so. My concern was that since the large portion of the Game Foundations is made with Scriptable Objects my assumption was that it should work in Editor mode too but I guess that's a larger and much deeper question in general for the Unity team.

    For now, this will solve the puzzle and will put a thought or two for the Unity team regards the Game Foundation package.

    Thank you for your response and have keep up with the great package.
     
    erika_d and mingz-unity like this.