Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    53
    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:
    53
    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:
    53
    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:
    53
    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,117
    Hi, Sorry to resurrect this thread. I'm having the exact same problem. How did you fix this?