[Solved] Loading screen not working after reload

Hi,

I’m working with the Unity IAP and I’m tring to add a Loading Screen. It works. But as soon as a reload the scene. I get the error : MissingReferenceException: The object of type ‘IAPShop’ has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object. But the purchasing still works and without the loader screen stuff, no error.

At line 132, I activate the loader panel and at 242, I call SetLoadingScreen witch is at 220 to desactivate it.

Thanks

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
// Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
public class IAPShop : MonoBehaviour, IStoreListener
{
    private static IStoreController m_StoreController;          // The Unity Purchasing system.
    private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
    public static string Product50000coins = "com....";
    public static string Product100000coins = "com...";
    public static string Product200000coins = "com...";
    public static string Product500000coins = "com...";
    public static string noads = "com...";
    public GameObject LoadingScreen;
    public static IAPShop Instance;
    public GameObject RestoreButton;
    void Awake()
    {
        Instance = this;
        // If we haven't set up the Unity Purchasing reference
        if (m_StoreController == null)
        {
            // Begin to configure our connection to Purchasing
            InitializePurchasing();
        }
        if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
        {
            RestoreButton.SetActive(true);
        }
        else {
            RestoreButton.SetActive(false);
        }
    }
    public ProductMetadata getMetadata(string ID)
    {
        return m_StoreController.products.WithID(ID).metadata;
    }
     
    public void InitializePurchasing()
    {
        // If we have already connected to Purchasing ...
        if (IsInitialized())
        {
            // ... we are done here.
            return;
        }
        // Create a builder, first passing in a suite of Unity provided stores.
        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
        // Add a product to sell / restore by way of its identifier, associating the general identifier
        // with its store-specific identifiers.
        builder.AddProduct(Product50000coins, ProductType.Consumable);
        builder.AddProduct(Product100000coins, ProductType.Consumable);
        builder.AddProduct(Product200000coins, ProductType.Consumable);
        builder.AddProduct(Product500000coins, ProductType.Consumable);
        builder.AddProduct(noads, ProductType.NonConsumable);
        // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
        // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
        UnityPurchasing.Initialize(this, builder);
    }
    private bool IsInitialized()
    {
        // Only say we are initialized if both the Purchasing references are set.
        return m_StoreController != null && m_StoreExtensionProvider != null;
    }
    public void Buy50000coins()
    {
        BuyProductID(Product50000coins);
    }
    public void Buy100000coins()
    {
        BuyProductID(Product100000coins);
    }
    public void Buy200000coins()
    {
        BuyProductID(Product200000coins);
    }
    public void Buy500000coins()
    {
        BuyProductID(Product500000coins);
    }
    public void BuyNoAds()
    {
        BuyProductID(noads);
    }
    void BuyProductID(string productId)
    {
        // If Purchasing has been initialized ...
        if (IsInitialized())
        {
            // ... look up the Product reference with the general product identifier and the Purchasing
            // system's products collection.
            Product product = m_StoreController.products.WithID(productId);
            // If the look up found a product for this device's store and that product is ready to be sold ...
            if (product != null && product.availableToPurchase)
            {
                Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
                // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
                // asynchronously.
                m_StoreController.InitiatePurchase(product);
                LoadingScreen.SetActive(true);
            }
            // Otherwise ...
            else
            {
                // ... report the product look-up failure situation
                Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
            }
        }
        // Otherwise ...
        else
        {
            // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
            // retrying initiailization.
            Debug.Log("BuyProductID FAIL. Not initialized.");
        }
    }
    // Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
    // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
    public void RestorePurchases()
    {
        // If Purchasing has not yet been set up ...
        if (!IsInitialized())
        {
            // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
            Debug.Log("RestorePurchases FAIL. Not initialized.");
            return;
        }
        // If we are running on an Apple device ...
        if (Application.platform == RuntimePlatform.IPhonePlayer ||
            Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
        {
            // ... begin restoring purchases
            Debug.Log("RestorePurchases started ...");
            // Fetch the Apple store-specific subsystem.
            var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
            // Begin the asynchronous process of restoring purchases. Expect a confirmation response in
            // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
            apple.RestoreTransactions((result) => {
                // The first phase of restoration. If no more responses are received on ProcessPurchase then
                // no purchases are available to be restored.
                Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
            });
        }
        // Otherwise ...
        else
        {
            // We are not running on an Apple device. No work is necessary to restore purchases.
            Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
        }
    }
    //
    // --- IStoreListener
    //
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        // Purchasing has succeeded initializing. Collect our Purchasing references.
        Debug.Log("OnInitialized: PASS");
        // Overall Purchasing system, configured with products for this application.
        m_StoreController = controller;
        // Store specific subsystem, for accessing device-specific store features.
        m_StoreExtensionProvider = extensions;
        getLocalPrice[] _getLocalPrice = FindObjectsOfType(typeof(getLocalPrice)) as getLocalPrice[];
        foreach(getLocalPrice LocalPrice in _getLocalPrice)
        {
            LocalPrice._localizePrice();
        }
    }
    public void OnInitializeFailed(InitializationFailureReason error)
    {
        // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
        Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    }
    IEnumerator SetLoadingScreen(bool _isActive)
    {
        yield return new WaitForSeconds (0.01f);
        GameObject ShopLoadingScreen =  GameObject.FindGameObjectWithTag("ShopLoadingScreen");
        ShopLoadingScreen.SetActive(false);
    }
    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    {
        // A consumable product has been purchased by this user.
        if (String.Equals(args.purchasedProduct.definition.id, Product50000coins, StringComparison.Ordinal))
        {...
        }
        else
        {
            Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
        }
        StartCoroutine(SetLoadingScreen(false));
        return PurchaseProcessingResult.Complete;
    }
    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    {
        // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
        // this reason with the user to guide their troubleshooting actions.
        Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
        LoadingScreen.SetActive(false);
    }
}

Hi @PhillippeC ,

That error means you are trying to reference an object after it has been destroyed. Does it give you a line number for the error? That would indicate exactly where you are referencing a destroyed object.

Since all game objects are destroyed after a scene load, if you have any objects that persist between scenes (via DontDestoryOnLoad) then they might be referencing an older instance that has been destroyed.