I’m trying to implement Unity IAP using JS and I get this error:
InvalidCastException: Cannot cast from source type to destination type.
The problem is this line (concretely “this”):
UnityPurchasing.Initialize(this, builder);
I use the sample Store from https://docs.unity3d.com/ScriptReference/Purchasing.Product.html
I’ve just added “import UnityEngine.Purchasing” that was missing:
#pragma strict
import UnityEngine.Purchasing;
public class MyStoreClass extends MonoBehaviour {
static var kProductID100Currency: String = "virtualcurrency_100";
var m_StoreController: IStoreController;
function Start() {
var builder: ConfigurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(kProductID100Currency, ProductType.Consumable);
UnityPurchasing.Initialize(this, builder);
}
public function PurchaseCurrency() {
if (m_StoreController != null) {
// Fetch the currency Product reference from Unity Purchasing
var product: Product = m_StoreController.products.WithID(kProductID100Currency);
if (product != null && product.availableToPurchase) {
m_StoreController.InitiatePurchase(product);
}
}
}
public function OnInitialized(controller: IStoreController, extensions: IExtensionProvider) {
m_StoreController = controller;
}
public function OnInitializeFailed(error: InitializationFailureReason) {
}
public function ProcessPurchase(e: PurchaseEventArgs) {
if (String.Equals(e.purchasedProduct.definition.id, kProductID100Currency, StringComparison.Ordinal)) {
Debug.Log("Purchased 100 coins");
}
return PurchaseProcessingResult.Complete;
}
public function OnPurchaseFailed(item: Product, r: PurchaseFailureReason) {
}
}
The C# example runs without errors. So my configuration seems to be OK.
In the C# example, MyStoreClass class derives from IStoreListener
public class Purchaser : MonoBehaviour, IStoreListener {
Could this be the problem? How I do that with JS?