IAP error NullReferenceException: Object reference not set to an instance of an object
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Purchasing;
public class IapManager : MonoBehaviour, IStoreListener { public static IapManager Instance { set; get; } public main gameMgrs; private static IStoreController m_StoreController; private static IExtensionProvider m_StoreExtensionProvider;
public const string productId10 = "marble_10_kor";
void Start()
{
if (m_StoreController == null)
{
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized())
{
return;
}
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(productId10, ProductType.Consumable);
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void Buy1()
{
BuyProductID(productId10);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
switch (args.purchasedProduct.definition.id)
{
case productId10:
Debug.Log("10개 구매완료");
gameMgrs.cash1();
break;
}
return PurchaseProcessingResult.Complete;
}
public void buy1() { Debug.Log(1); IapManager.Instance.Buy1(); Debug.Log(2); } Debug 1
I want use IAP. but have some error
NullReferenceException: Object reference not set to an instance of an object
Did I do something wrong?
Answer by JimmyHo · May 19, 2017 at 02:48 PM
I also encounter this problem on android. My unity version is 5.5.0f3.. Anyone help?
I find sollution!!
void Awake() { Instance = this; } plus this code
Answer by tangyinglin0228 · Apr 09, 2020 at 04:21 PM
The tutorial is confusing. I just came into the same problem today. After researching and looking around the whole day. I could finally figure out what's wrong. m_StoreController is indeed not initialized until the IStoreListener method returns it.
public void OnInitialized(IStoreController controller, IExtensionProvider extensions) {
m_StoreController = controller;
}
These 3 lines of code should be put into awake as well or they would never be called and thus m_StoreController will never get initialized.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(productId10, ProductType.Consumable);
UnityPurchasing.Initialize(this, builder);
Your answer
Follow this Question
Related Questions
cannot convert from 'Unity.Entities.Entity' to 'Unity.Entities.EntityQuery' 0 Answers
Can't add Orbital script - "The script needs to derive from MonoBehaviour!" 0 Answers
I can't store raycast hit data in a variable?! 0 Answers
how to switch in multiple camera's? 0 Answers
Respawn Script Is Not Functioning 0 Answers