Search Unity

UnityAds not ready after realoading scene.

Discussion in 'Unity Ads & User Acquisition' started by Victorv88, Mar 25, 2020.

Thread Status:
Not open for further replies.
  1. Victorv88

    Victorv88

    Joined:
    Mar 20, 2020
    Posts:
    4
    I implement UnityAds on my mobile game, and they work just fine, for first time that the scene is load, but as soon as my restart button (which reload the scene) is pressed, i can't get my ads ready anymore.
    Any clues ?
     
  2. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    When you reload a scene you need to make sure your IUnityAdsListener class either persists into the new scene, or you will need to create a new instance of that class. Reloading a scene destroys all components from the previous scene and it could be that this includes your IUnityAdsListener class.

    You should also remember to make sure IUnityAdsListener classes always remove themselves as a listener when they are disabled or destroyed. You can do that with the following code:
    Code (CSharp):
    1. private void OnDisable()
    2. {
    3.     Advertisement.RemoveListener( this );
    4. }
    If you still have problems it would be useful to see your code.
     
    Abykak and cidacoder like this.
  3. Victorv88

    Victorv88

    Joined:
    Mar 20, 2020
    Posts:
    4

    I still do have the problem. At the end of my game, in order to restart the game, i use SceneManager.LoadScene(0);
    And when reloading the scene, i do exactly the same initialization as the first run :
    Code (CSharp):
    1.  public void initAds()
    2.     {
    3.         Debug.Log("InitAd");
    4.         Advertisement.AddListener(this);
    5.         Advertisement.Initialize(gameId, testMode);
    6.     }
    But seems like when i reinitialize the scene, it doesn't run that part:
    Code (CSharp):
    1. public void OnUnityAdsReady(string placementId)
    2.     {
    3.         // If the ready Placement is rewarded, show the ad:
    4.         if (placementId == myPlacementId)
    5.         {
    6.             isAdReady = true;
    7.         }
    8.     }
    Is there something i can do ?

    Thank you.
     
  4. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    You only need to initialize the Ads SDK once per game session. It remains active across all scenes.

    What I think is happening is this:
    • An ad plays in your game.
    • While that ad is playing, the SDK starts loading a new ad for that placement.
    • The SDK ready event fires.
    • You call SceneManager.LoadScene(0) sometime later.
    • The SDK Initialize call happens, but the SDK knows it's already initialised so does nothing.
    • The OnUnityAdsReady function never gets called because the Ready event already happened before you reloaded the scene!
    You can check Advertisement.isInitialized to see if the SDK is already initialized.

    Also, you don't need to manually track isAdReady in your code - you can check Advertisement.IsReady(placementId) for this.

    Hope that helps!
     
    pablolluchr and Tx like this.
  5. Victorv88

    Victorv88

    Joined:
    Mar 20, 2020
    Posts:
    4
    It worked!! Thank you!!
    Now i have another problem. When reloading scene i get MissingReffereceException on public GameObject variables. Any clues?

    Thank you.
     
  6. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    It's impossible to say without looking at your project, but it sounds like your scripts are trying to refer to objects from old scenes that have been destroyed when that scene got unloaded. You might have to update the reference to point towards the new scene's version of those objects.
     
  7. goodgamershow

    goodgamershow

    Joined:
    Apr 5, 2021
    Posts:
    15
    I have the same exact problem like yours. Could you please describe the steps you followed to make OnUnityAdsReady method work after restarting the scene?
     
  8. Nikhil_3020

    Nikhil_3020

    Joined:
    Oct 15, 2020
    Posts:
    2
    I m having an issue or i dont know like if my game is in test mode and i try to play a rewarded ad then it appears but it dont print or execute the function inside where the user gets the Reward like how will I get to know if it really works or not
     
  9. Cloud-Yo

    Cloud-Yo

    Joined:
    Sep 5, 2014
    Posts:
    12
    Version 4.0 of the Unity ads deprecated the RemoveListener method. Now I'm unable to remove listeners and the Rewarded ads OnCompleted callbacks are firing off multiple times (incrementally) as if each time it ads one more listener to the Queue. Any advice on how to remove the previous listeners? I'm using the script provided by Unity but I'm not implementing the ads through a button, rather through a method call with a delegate callback.
     
  10. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Cloud-Yo likes this.
  11. Cloud-Yo

    Cloud-Yo

    Joined:
    Sep 5, 2014
    Posts:
    12
    D12294 likes this.
Thread Status:
Not open for further replies.