Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Missing Reference Exception using Unity Ads on second run of game

Discussion in 'Unity Ads & User Acquisition' started by williamxhal, Aug 9, 2023.

  1. williamxhal

    williamxhal

    Joined:
    Aug 8, 2023
    Posts:
    2
    I'm building a game which uses Ads that run when a player returns to the main menu. The first time I run the game the ads run as intended and the script works, however the second time I try to run it, it produces the following error message:

    MissingReferenceException: The object of type 'GameObject' 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.
    UnityEngine.Advertisements.Placeholder.ShowSkipButton (UnityEngine.GameObject canvasGameObject) (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Platform/Editor/Placeholder.cs:187)
    UnityEngine.Advertisements.Placeholder.Show (System.String placementId, System.Boolean allowSkip) (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Platform/Editor/Placeholder.cs:74)
    UnityEngine.Advertisements.Platform.Editor.EditorPlatform+<>c__DisplayClass14_0.<Show>b__0 () (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Platform/Editor/EditorPlatform.cs:165)
    UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@4.4.2/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)

    Using Debug.Log I suspect the ads may not be initializing properly the second time I run the game but have no clue on what the actual problem is.
    I am using Unity Ads (not mediation/Levelplay) and have updated my ads to the most recent version. My Ads script is as follows:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener, IUnityAdsInitializationListener
    8. {
    9.     [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    10.     string _adUnitId;
    11.  
    12.     public bool isTestingMode = true;
    13.  
    14.     void Start()
    15.     {
    16.         StartCoroutine(DelayedInitialization());
    17.     }
    18.  
    19.     IEnumerator DelayedInitialization()
    20.     {
    21.         yield return new WaitForSeconds(2.0f); // Adjust the delay as needed
    22.         Advertisement.Initialize("********", isTestingMode, this);
    23.     }
    24.  
    25.     void Awake()
    26.     {
    27.  
    28.         // Get the Ad Unit ID for the current platform:
    29.         _adUnitId = _iOsAdUnitId;
    30.  
    31.         LoadAd();
    32.     }
    33.  
    34.     // Load content to the Ad Unit:
    35.     public void LoadAd()
    36.     {
    37.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    38.         Debug.Log("Loading Ad: " + _adUnitId);
    39.         Advertisement.Load(_adUnitId, this);
    40.     }
    41.  
    42.     // Show the loaded content in the Ad Unit:
    43.     public void ShowAd()
    44.     {
    45.         Debug.Log("Showing Ad: " + _adUnitId);
    46.         Advertisement.Show(_adUnitId, this);
    47.     }
    48.  
    49.     // Implement Load Listener and Show Listener interface methods:
    50.     public void OnUnityAdsAdLoaded(string adUnitId)
    51.     {
    52.         // Optionally execute code if the Ad Unit successfully loads content.
    53.     }
    54.  
    55.     public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
    56.     {
    57.         Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
    58.         // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
    59.     }
    60.  
    61.     public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    62.     {
    63.         Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
    64.     }
    65.  
    66.     public void OnUnityAdsShowStart(string _adUnitId) { }
    67.     public void OnUnityAdsShowClick(string _adUnitId) { }
    68.     public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState)
    69.     {
    70.         SceneManager.LoadScene("MainMenu");
    71.     }
    72.  
    73.  
    74.     public void OnInitializationComplete()
    75.     {
    76.         print("Ads initialized!!");
    77.     }
    78.  
    79.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    80.     {
    81.         print("failed to initialize!!");
    82.     }
    83. }
    I appreciate the code has not been written in the most optimal way but have tried various different methods of using the Ads and the same problem always arises.

    Thank you for any help (I hope I have posted this issue in the correct place, I apologies if not)
     
  2. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    Could you check if this error will also occur on a real device?
     
  3. williamxhal

    williamxhal

    Joined:
    Aug 8, 2023
    Posts:
    2
    Having built my game for iOS and played it on an iphone, this issue no longer appears. Thank you very much.
     
    SamOYUnity3D likes this.
  4. hydraxic

    hydraxic

    Joined:
    Mar 30, 2022
    Posts:
    2
    This error also happens for me in the editor. I haven't tried building it yet like @williamxhal did, but it is very annoying in the editor.

    Edit: it randomly started working again. not sure why
     
    Last edited: Dec 3, 2023