0
\$\begingroup\$

I'm trying to implement IAP in my game where the user can buy 100 coins. I have used this video tutorial to implement the IAP and this works fine. I want to implement a functionaltiy where once the purchase has been made, a panel opens up stating that the purchase was successful. This is the code in the IAP manager that handles a successful purchase:

public void Coin_100()
{
    BuyProductID(coin_100);
}

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
    if (String.Equals(args.purchasedProduct.definition.id, coin_100, StringComparison.Ordinal))
    {
        GameManager.currentCoinScore += 100;           
    }
}

And here is the PurchaseButton script and I call the Coin100 method when the user click on the purchase button:

public void Coin100() {
    IAPManager.instance.Coin_100();
    PurchaseSuccess.SetActive(true);
}
    

The issue is that as soon as the the button is clicked the panel opens up. Is there any way to delay activating the panel i.e once the purchase is successful and not as soon as the button is clicked. I thought of adding a static bool to the IAP script and then in the Update function of the PurchaseButton script, check if this bool is true and then activate the panel. But this effects the performance. Can someone please help me with this?

\$\endgroup\$
2
  • \$\begingroup\$ How about put PurchaseSuccess.SetActive(true); after GameManager.currentCoinScore += 100;? \$\endgroup\$
    – Mangata
    Commented Jan 16, 2023 at 13:44
  • \$\begingroup\$ this is not working as the IAP manager persists across scenes. And when the main game is loaded, I get a null reference error as the success panel doesn't exist in the main game. Its only in the level selection screen where the IAP manager script resides. Is there any way to use an eventhandler or action to implement this? \$\endgroup\$ Commented Jan 17, 2023 at 13:23

1 Answer 1

1
\$\begingroup\$

First, remove the "PurchaseSuccess.SetActive(true);" from Coin100 method. Create a new public method called ShowSuccessDialog and add the code there.

Then, in your IAP manager, create a new event, and add your GameManager function as a listener for the event.

UnityEvent m_PurchaseSuccess = new UnityEvent();

void Start()
{
    m_PurchaseSuccess.AddListener(FindObjectOfType<GameManager>().ShowSuccessDialog);
}

Then the event can be invoked at the proper time and the listening method will be executed. In your PurchaseProcessingResult, after adding 100 coins, invoke the event like this:

m_PurchaseSuccess.Invoke();

The ShowSuccessDialog is listening for the event to fire, then the dialog should show once the purchase process is finished.

I haven't tested this code, but here's the documentation on Unity events if you're still stuck: https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html

\$\endgroup\$
1
  • \$\begingroup\$ thanks for the response...I will try to implement this in my game. \$\endgroup\$ Commented Jan 19, 2023 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .