question

maria-1 avatar image
maria-1 asked

Economy V2 Adding item to inventory not working

Hello, I am trying to make this class reusable and depending on the "FriendlyId" of the item in the store Playfab will find it and that item will be bought and added to the players inventory. I am unsure why it isnt working though. The console does not give me any errors but it also doesnt do anything from my knowledge. I am using unity and C# to do this. Any advice or help would be greatly appreciated. I feel as though I very close to getting it working.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.EconomyModels;

public class ItemToBuy : MonoBehaviour
{
    public int glimmersPrice;
    public string itemName;
    
    public void BuyItem2() {
        var request = new AddInventoryItemsRequest {
            Amount = 1,
            Item = new InventoryItemReference
            {
                AlternateId = new PlayFab.EconomyModels.AlternateId {Type = "FriendlyId", Value = "{ItemName}"}
            }
        };
        PlayFabEconomyAPI.AddInventoryItems(request, OnBuyItemSuccess, OnError);
    }

    void OnBuyItemSuccess(AddInventoryItemsResponse response){
        Debug.Log("Bought item! " + itemName);
    }


     void OnError(PlayFabError error) {
       Debug.Log("Error: " + error.ErrorMessage);
    }
}
apisIn-Game Economysupport
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

Your code should be correct, in my test, it works fine. Since you have mentioned that “The console does not give me any errors but it also doesnt do anything from my knowledge.” If that means both callbacks are not triggered, please test with Postman first to avoid coding issue.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.