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 :
using UnityEngine;
using UnityEngine.SceneManagement;
public class RestartController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void RestartLevel(){
SceneManager.LoadScene(0);
}
}
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.
Initialise Unity IAP inside an awake method (that only gets called once) and use DontDestroyOnLoad to stop your listener being destroyed on scene change.
using UnityEngine;
using UnityEngine.Purchasing;
class MyStoreListener : MonoBehaviour, IStoreListener {
public void Awake()
{
DontDestroyOnLoad(gameObject);
// Initialize Unity IAP here.
}
// Implementation of IStoreListener...
}
@analytic FYI an alternative Singleton approach exists (longer compared to @Banderous ’ elegant approach of using DontDestroyOnLoad) in this IAP Samples repository Bitbucket.
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