Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Ads video doesn't work after reloading scene

Discussion in 'Unity Ads & User Acquisition' started by PhobosRandom, Nov 12, 2021.

  1. PhobosRandom

    PhobosRandom

    Joined:
    May 13, 2020
    Posts:
    20
    Hi, so basically I have a button to load an ads video, first time I launch the scene, it works like a charm. But as I stop then restart the scene, it doesn't work anymore and it throws to me this 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.HideSkipButton (UnityEngine.GameObject canvasGameObject) (at Library/PackageCache/com.unity.ads@3.7.5/Runtime/Advertisement/Platform/Editor/Placeholder.cs:179)
    UnityEngine.Advertisements.Placeholder.Show (System.String placementId, System.Boolean allowSkip) (at Library/PackageCache/com.unity.ads@3.7.5/Runtime/Advertisement/Platform/Editor/Placeholder.cs:80)
    UnityEngine.Advertisements.Platform.Editor.EditorPlatform+<>c__DisplayClass15_0.<Show>b__0 () (at Library/PackageCache/com.unity.ads@3.7.5/Runtime/Advertisement/Platform/Editor/EditorPlatform.cs:188)
    UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@3.7.5/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)

    but still also debugging that the ad was played, even tho it wasn't.
    And if I again stop then restart the scene, it doesn't throw anymore the error message, only the successful debug, but still no ads video at all.

    I saw a thread where a person had the same problem, but I didn't see any fix there that worked for me (https://forum.unity.com/threads/unity-video-ads-work-until-i-reload-scene.803940/)

    I really don't understand why it's doing this, so if any of you has an idea on how to fix this, it'll be greatly welcomed.

    Here is my code if any of you want to see it :
    Code (CSharp):
    1. public class AdsManager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    2. {
    3. #if UNITY_IOS
    4.     string gameID = "myIosId";
    5. #else
    6.     string gameID = "myAndroidId";
    7. #endif
    8.  
    9.     [SerializeField] Button showAdButton;
    10.  
    11.     void Awake()
    12.     {
    13.         if (!Advertisement.isInitialized)
    14.         {
    15.             Advertisement.Initialize(gameID, true, true, this);
    16.         }
    17.         else
    18.         {
    19.             Debug.Log("already initialize");
    20.             LoadAd();
    21.         }
    22.  
    23.         //showAdButton.interactable = false;
    24.     }
    25.  
    26.     public void LoadAd()
    27.     {
    28.         Debug.Log("Loading Ad: " + gameID);
    29.         Advertisement.Load("rewardedVideo", this);
    30.     }
    31.  
    32.     public void ShowAd()
    33.     {
    34.         Debug.Log("play ad");
    35.  
    36.         Advertisement.Show("rewardedVideo");
    37.  
    38.         LoadAd();
    39.     }
    40.  
    41.     public void OnInitializationComplete()
    42.     {
    43.         Debug.Log("Unity Ads initialization complete.");
    44.         LoadAd();
    45.     }
    46.  
    47.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    48.     {
    49.         Debug.Log("Unity Ads initialization complete.");
    50.     }
    51.  
    52.     public void OnUnityAdsAdLoaded(string placementId)
    53.     {
    54.         Debug.Log("Ad Loaded: " + placementId);
    55.  
    56.         if (placementId.Equals("rewardedVideo"))
    57.         {
    58.             //showAdButton.interactable = true;
    59.         }
    60.     }
    61.  
    62.     public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
    63.     {
    64.         Debug.Log($"Error loading Ad Unit {placementId}: {error.ToString()} - {message}");
    65.     }
    66.  
    67.     public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
    68.     {
    69.         Debug.Log($"Error showing Ad Unit {placementId}: {error.ToString()} - {message}");
    70.     }
    71.  
    72.     public void OnUnityAdsShowStart(string placementId)
    73.     {
    74.     }
    75.  
    76.     public void OnUnityAdsShowClick(string placementId)
    77.     {
    78.     }
    79.  
    80.     public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
    81.     {
    82.     }
    83. }
     
    Last edited: Nov 12, 2021
  2. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    Hi @PhobosRandom, thank you for reaching out! Sorry to hear about this!

    First, I'd like to clarify, is this happening in the editor, emulator, or on a physical device? Is this only happening with test ads or with production ads as well? Which device model and OS are you testing with?

    Which version of Unity are you using? Which version of the ads SDK?

    I posted an answer on this thread that sounds similar that may help you, but it is also possible that this is a separate issue. I hope it helps!
     
  3. PhobosRandom

    PhobosRandom

    Joined:
    May 13, 2020
    Posts:
    20
    The bug is happening in the editor. When I test on a physical device nothing happens (no bugs, but no ads). I've only tested it with the test ads for now.
    The phone I use for testing is a Oneplus 6 on android 11.
    The computer I use is a Dell G3 15 on windows 10.

    I'm testing on unity 2020.3.21f1
    I'm using the ads SDK 3.7.5

    And all the objects I used for the ads system are already on a DontDestroyOnLoad
     
    Last edited: Nov 17, 2021
  4. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    Thanks for the additional info.

    I haven't been able to reproduce the issue you're experiencing in the editor using Unity 2020.3.21f1. Would you please create a ticket with us and, ideally, include a project we can use to reproduce the issue?

    I took another look at your code.
    • I noticed that you added a Load call in your Show function. If you're looking to load the next ad immediately after showing, I recommend moving your Load call to the OnUnityAdsShowComplete callback.
    • I also noticed that you've commented out your logic related to the showAdButton. This could be a reason why you do not see any ads when testing on a physical device. Given that I'm not familiar with your complete implementation logic, it is hard to say without a device log or reproducing the issue. Again, creating a support ticket with us and including a build of your app or store link we can test with will help us gather the information we'd need for a more thorough investigation into what's happening.
    When creating your support ticket, please also feel free to link to this forum thread to give the agent some additional context.
     
  5. PhobosRandom

    PhobosRandom

    Joined:
    May 13, 2020
    Posts:
    20
    Okay, I'll try to do that, I just don't know how to create a ticket.
    Also for the project I need to post with the ticket, you want the project zipped, or a build? And if a build, an exe or an apk ?
     
  6. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    Oh, sorry about that! I should've been more clear.

    There are two ways to create a ticket:
    1. Click on the question mark (?) icon at the top right of the dashboard. Scroll down to the file a ticket section. Monetization -> Monetize
    2. Email unityads-support@unity3d.com
    In this case, ideally, we'll want to see your Unity project. You can zip the project folder and attach it to the ticket or provide us with a Google Drive link if that's easier.

    Thanks! :)
     
  7. PhobosRandom

    PhobosRandom

    Joined:
    May 13, 2020
    Posts:
    20
    Okay thanks a lot for your help :), I've just sent the ticked.
     
    Unity_Quinn likes this.
  8. tbiz5270

    tbiz5270

    Joined:
    Sep 11, 2014
    Posts:
    18
    I am still receiving this error with Unity 2019.4.39f1 and Unity Ads 4.2.1.

    I am receiving the same error while testing in the editor (haven't tested on mobile yet, but I assume it will be the same result as OP where an ad simply does not display because of the error). I will test on mobile shortly and edit with my findings.

    This is occurring because the Advertisement.isInitialized flag saves through game (or scene) restarts.

    The first time I run my game, Advertisement.isInitialized is false (which is correct, because i just started the game), so it correctly initializes. An ad is then loaded and showed correctly.

    However, if I stop and re-run the game, Advertisement.isInitialized is already set as true (but it should not be, because I literally just started a fresh run of the game). For this reason, Advertisement.Initialize does nothing, because it thinks it is already initialized. An ad is still loaded when I call Advertisement.Load, but it will not display because of the error OP mentioned.

    The Advertisement.isInitialized will remain true until I make a change to its script, forcing Unity to recompile it. It will then be false again when I run the game, and everything will initialize, load, and display correctly.

    EDIT: After testing on mobile (both iOS and Android), everything seems to be working fine. Advertisement.isInitialized is correctly reset to false after each game restart, allowing it to re-initialize correctly, and therefore ads are successfully loaded and displayed correctly. This problem appears to only be present in the Unity Editor. I suppose stopping the game in the editor behaves differently than exiting the app on mobile, and so doesn't reset the flag correctly.
     
    Last edited: Jul 7, 2022
  9. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    play mode options may cause it. if you really really need to see the result in editor mode you can temporarily uncheck it by killing your editor performance (I meant very long play button time). I reported it and Unity didn't care about it too much.
    You can also don't care about it too much and you can test your ads in Android platform not in Editor. Besides you can also don't use Unity ads there are lots of other alternatives that I believe much easier to implement.

    upload_2022-7-12_4-52-24.png
     
    SKizil likes this.