Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity IAP with JS (InvalidCastException)

Discussion in 'Scripting' started by monremobile, Dec 17, 2015.

  1. monremobile

    monremobile

    Joined:
    Sep 16, 2015
    Posts:
    10
    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:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.Purchasing;
    4.  
    5. public class MyStoreClass extends MonoBehaviour {
    6.     static var kProductID100Currency: String = "virtualcurrency_100";
    7.     var m_StoreController: IStoreController;
    8.     function Start() {
    9.         var builder: ConfigurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    10.         builder.AddProduct(kProductID100Currency, ProductType.Consumable);
    11.         UnityPurchasing.Initialize(this, builder);
    12.     }
    13.     public function PurchaseCurrency() {
    14.         if (m_StoreController != null) {
    15.             // Fetch the currency Product reference from Unity Purchasing
    16.             var product: Product = m_StoreController.products.WithID(kProductID100Currency);
    17.             if (product != null && product.availableToPurchase) {
    18.                 m_StoreController.InitiatePurchase(product);
    19.             }
    20.         }
    21.     }
    22.     public function OnInitialized(controller: IStoreController, extensions: IExtensionProvider) {
    23.         m_StoreController = controller;
    24.     }
    25.     public function OnInitializeFailed(error: InitializationFailureReason) {
    26.     }
    27.     public function ProcessPurchase(e: PurchaseEventArgs) {
    28.         if (String.Equals(e.purchasedProduct.definition.id, kProductID100Currency, StringComparison.Ordinal)) {
    29.             Debug.Log("Purchased 100 coins");
    30.         }
    31.         return PurchaseProcessingResult.Complete;
    32.     }
    33.     public function OnPurchaseFailed(item: Product, r: PurchaseFailureReason) {
    34.     }
    35. }

    The C# example runs without errors. So my configuration seems to be OK.
    In the C# example, MyStoreClass class derives from IStoreListener
    Code (CSharp):
    1.  public class Purchaser : MonoBehaviour, IStoreListener {
    Could this be the problem? How I do that with JS?
     
  2. ignitiongames

    ignitiongames

    Joined:
    Feb 27, 2015
    Posts:
    20
    Have you had any luck with this? I too am trying to get this to work but am stuck on the same error you're getting.
     
  3. monremobile

    monremobile

    Joined:
    Sep 16, 2015
    Posts:
    10
    No, I haven't. Sorry.
    Maybe you can use the Unity IAP C# script in combination with SendMenssage() from your JS scripts.

    This is the reason I'm migrating to C#.....the JS script reference and support by Unity and other plugins is very poor.
    I liked JS much more...it was faster to type :(...
     
  4. Banderous

    Banderous

    Joined:
    Dec 25, 2011
    Posts:
    669
    Your class needs to implement the IStoreListener interface:

    Code (CSharp):
    1. public class MyStoreClass extends MonoBehaviour implements IStoreListener {
    2. ...
     
    monremobile likes this.