Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved UnityServices.InitializeAsync + ConfigurationBuilder.Instance Crash on Android

Discussion in 'Unity IAP' started by JJunior, Mar 23, 2023.

  1. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    54
    I am using Unity 2020.3.43f1 and Unity IAP 4.7.0

    I did a test app where I have two buttons, the first button calls the UnityServices initialization:

    Code (CSharp):
    1. public void InitUnityServices()
    2.     {
    3.         try
    4.         {
    5.             var options = new InitializationOptions().SetEnvironmentName("production");
    6.             UnityServices.InitializeAsync(options).ContinueWith(task => OnUnityServicesInitializeSuccess());
    7.         }
    8.         catch (Exception e)
    9.         {
    10.             OnUnityServicesInitializeError(e.Message);
    11.         }
    12.     }
    13.  
    14.     private void OnUnityServicesInitializeSuccess()
    15.     {
    16.         Debug.Log($"UnityServices Initialization Success");
    17.     }
    18.  
    19.     private void OnUnityServicesInitializeError(string message)
    20.     {
    21.         Debug.Log($"UnityServices Initialization Error: {message}");
    22.     }
    The second button calls the IAP initialization:

    Code (CSharp):
    1. public void InitializePurchasing()
    2.     {
    3.         ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    4.  
    5.         builder.AddProduct("com.myappdomain.coinpack1", ProductType.Consumable);
    6.  
    7.         UnityPurchasing.Initialize(this, builder);
    8.     }
    Everything works fine if I click the buttons one after another, but if I put the IAP initialization on the Unity Services initialization callback the app crashes on Android:

    Code (CSharp):
    1. private void OnUnityServicesInitializeSuccess()
    2.     {
    3.         Debug.Log($"UnityServices Initialization Success");
    4.  
    5.         InitializePurchasing();
    6.     }
    The crash happens on ConfigurationBuilder.Instance line.

    Any idea what is happening? Should I workaround with some kind of delay?

    Any help is appreciated, I am running out of time to publish the app :S
     
  2. NoPants_

    NoPants_

    Joined:
    Apr 23, 2014
    Posts:
    59
    I think you can just await "UnityServices.InitializeAsync(options)." This is what I do. Note the async in the init. Then await the UnityServices.

    Code (CSharp):
    1. private async void Init()
    2.         {
    3.             var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    4.  
    5.               ... add products here.
    6.  
    7.             await UnityServices.InitializeAsync();
    8.             UnityPurchasing.Initialize(this, builder);
    9.         }
     
    Last edited: Mar 23, 2023
    Arnaud_Gorain and JJunior like this.
  3. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    54
    I tried with an async method the result is the same - native crash on Android. Not exactly the same, the crash looks to be intermittent.

    Code (CSharp):
    1. public async void InitUnityServices()
    2.     {
    3.         Debug.Log($"InitUnityServices");
    4.         var options = new InitializationOptions().SetEnvironmentName("production");
    5.         await UnityServices.InitializeAsync(options);
    6.  
    7.         InitializePurchasing();
    8.     }
    9.  
    10.     public void InitializePurchasing()
    11.     {
    12.         Debug.Log($"InitializePurchasing");
    13.  
    14.         if (_storeController != null) return;
    15.  
    16.         ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    17.  
    18.         builder.AddProduct("com.myappdomain.coinpack1", ProductType.Consumable);
    19.  
    20.         UnityPurchasing.Initialize(this, builder);
    21.     }
    In the editor it works just fine.
     
  4. NoPants_

    NoPants_

    Joined:
    Apr 23, 2014
    Posts:
    59
    Are you using a button to set all this up? I have a class that I set up on Awake(), set it to DontDestroyOnLoad, and store the instance in a public singleton. If you press both buttons too fast, the await might not have finished yet.
     
  5. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    54
    I am trying a different approach @NoPants_. I am initializing the Unity Services on the first scene of my game, and the Unity Purchasing on the second one, I think this is going to work. Thank you a lot for trying to help me. I do not know, I think this may be an issue with Unity Services + Unity Purchasing on Android, who knows.
     
  6. NoPants_

    NoPants_

    Joined:
    Apr 23, 2014
    Posts:
    59
    I don't think that's the problem tbh. Async can be a bit tricky. You may find it works sometimes, but not always with the way you are talking about setting it up. But if you test it and it does work all the time, I guess it doesn't matter.
     
  7. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    54
    It's working now... I was using async at first, and I had to change because the apps was rejected once it was crashing on the reviewers devices... This is weird, but at least it seems to work all the times. Thank you again man!
     
  8. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,123
    Hi, Sorry to resurrect this thread. I'm having the exact same problem. How did you fix this?