Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

UnityAds not ready after realoading scene.

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

  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. kr-unity

    kr-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    44
    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.
     
  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. kr-unity

    kr-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    44
    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!
     
  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. kr-unity

    kr-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    44
    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.
     
unityunity