I’m using the codeless API to make my buttons, but I need to instantiate the buttons via script. So far, I am able to get it mostly working by making the button and adding a IAPButton.cs script to it, then disabling the button. I then set up the type of button, set it’s productID etc. via my own script. Finally, I enable the button.
What I can’t seem to do is to add a listener to the new button. This post seemed the most helpful, adding a delegate to the button. Here is the relevant code:
IAPButton iapButton = btn.gameObject.AddComponent<IAPButton>() as IAPButton;
iapButton.enabled = false;
iapButton.productId = btn.m_item.m_itemID;
iapButton.priceText = btn.m_priceLabel;
iapButton.titleText = btn.m_descriptionLabel;
iapButton.consumePurchase = false;
iapButton.buttonType = IAPButton.ButtonType.Purchase;
iapButton.onPurchaseComplete.AddListener(delegate { btn.ActivateNewPurchase(); });
iapButton.enabled = true;
}
The code compiles, but when running I get a “NullReferenceException: Object reference not set to an instance of an object” error on line 10.
What am I doing wrong?