Search Unity

[Solved] Unity IAP - Android - initialize error on Scene reload

Discussion in 'Unity IAP' started by chnaw, Jan 14, 2016.

Thread Status:
Not open for further replies.
  1. chnaw

    chnaw

    Joined:
    Oct 21, 2015
    Posts:
    7
    Hello,

    I am starting to look into Unity IAP service, and I am stuck on the following issue :
    - Each time the button reload level (SceneManager.LoadScene(0)) the IAP script throw this error :
    Unity IAP works fine until the button reload level is pressed.

    These are the steps I have followed:
    - I have created an empty new project
    - I have enabled the Unity IAP services and imported all the required files
    - I have setup everything required on Google Play side
    - I have added 2 buttons : "No Ads" button in order to make the purchase, and a "Reload" button that reload the current Scene
    - I have created a script for IAP following exactly what is inside the UnityPurchasing/script/IAPDemo.cs file
    - I have created a new empty Game Object on the current Scene
    - I have attached my own IAP script on this Game Object
    - I have created a simple Script to reload the current Scene and attached it to the Reload Button :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class RestartController : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.    
    16.     public void RestartLevel(){
    17.         SceneManager.LoadScene(0);
    18.     }
    19. }
     

    Attached Files:

  2. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    You should initialise Unity IAP only once, preferably in the first scene of your App. You should not reinitialise it every time a scene loads; your IStoreListener should be a singleton that exists for the life of your App.
     
  3. analytic

    analytic

    Joined:
    Jun 23, 2014
    Posts:
    5
    i have same problem but i realy dont know how to use singleton IStoreListener.

    Could you please add demo script how to use it ?

    thanks
     
  4. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    Initialise Unity IAP inside an awake method (that only gets called once) and use DontDestroyOnLoad to stop your listener being destroyed on scene change.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Purchasing;
    4.  
    5. class MyStoreListener : MonoBehaviour, IStoreListener {
    6.     public void Awake()
    7.     {
    8.         DontDestroyOnLoad(gameObject);
    9.         //  Initialize Unity IAP here.
    10.     }
    11.     // Implementation of IStoreListener...
    12. }
    13.  
     
    nicholasr likes this.
  5. nicholasr

    nicholasr

    Joined:
    Aug 15, 2015
    Posts:
    183
  6. chnaw

    chnaw

    Joined:
    Oct 21, 2015
    Posts:
    7
    Thanks a lot for your helps guys, it helped me a lot :)
     
    nicholasr likes this.
  7. analytic

    analytic

    Joined:
    Jun 23, 2014
    Posts:
    5
    problem solved. Thanks
     
    nicholasr likes this.
  8. SergeMoskalenko

    SergeMoskalenko

    Joined:
    Nov 13, 2014
    Posts:
    2
    Problem solved but not complete )) Item list is empty (in middle window) may be because my version not compatible? Unity 5.3.4f1 Help please resolve problem



    win.jpg emu.jpg win.jpg emu.jpg win.jpg
     
  9. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    Are you running IAP in an emulator? It won't work in an emulator, you need to run it on a device or in the Editor.
     
  10. SergeMoskalenko

    SergeMoskalenko

    Joined:
    Nov 13, 2014
    Posts:
    2
    Its my fault ! ))
    Now It work great!!!
    Thank You!!!
     
    erika_d likes this.
Thread Status:
Not open for further replies.