Search Unity

[Closed] Using Codeless API to trigger purchase event

Discussion in 'Unity IAP' started by Voronoi, Feb 4, 2018.

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

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    590
    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:

    Code (CSharp):
    1.  
    2. IAPButton iapButton = btn.gameObject.AddComponent<IAPButton>() as IAPButton;
    3. iapButton.enabled = false;
    4. iapButton.productId = btn.m_item.m_itemID;
    5. iapButton.priceText = btn.m_priceLabel;
    6. iapButton.titleText = btn.m_descriptionLabel;
    7. iapButton.consumePurchase = false;
    8. iapButton.buttonType = IAPButton.ButtonType.Purchase;
    9.  
    10. iapButton.onPurchaseComplete.AddListener(delegate { btn.ActivateNewPurchase(); });
    11. iapButton.enabled = true;
    12. }
    13.                
    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?
     
  2. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    590
    My first issue was an obvious one – the ActivateNewPurchase() function needed to accept the Product object. So it should be ActivateNewPurchase(Product p) and not a function with no parameters.

    Then, I need to put the IAPButton script on my button while it's a prefab. I can then alter the product.id and after making the UpdateText() function public, I call it to update everything. Works OK, but still not sure why I can't just create it at runtime.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  4. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    590
    Yes, I have gradually realized that using Codeless is creating problems for me since I am doing several things via code. I did go through that tutorial and I think the IAPDemo is also going to help me figure this out.
     
Thread Status:
Not open for further replies.