Search Unity

Advertisement Package error when load a new scene

Discussion in 'Unity Ads & User Acquisition' started by antony_morar, Mar 10, 2020.

  1. antony_morar

    antony_morar

    Joined:
    Nov 7, 2017
    Posts:
    12
    I'm using the advertising package v3.4.2. All works fine the first time in the game but when I change the scene or reload the same scene appears a MissingReferenceException. But only happends in the Advertisement code, if i try to acces the same object with other script works fine.

    The error:

    MissingReferenceException: The object of type 'ParticleSystem' 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.ParticleSystem.Play () (at /Users/builduser/buildslave/unity/build/Modules/ParticleSystem/ScriptBindings/ParticleSystem.bindings.cs:132)
    DiamondsAdded.PlayDiamondsAnimation () (at Assets/Scripts/UI/DiamondsAdded.cs:12)
    AdMultiplyGems.OnUnityAdsDidFinish (System.String placementId, UnityEngine.Advertisements.ShowResult showResult) (at Assets/Scripts/AdServices/AdMultiplyGems.cs:62)
    UnityEngine.Advertisements.Platform.Platform+<>c__DisplayClass41_0.<UnityAdsDidFinish>b__0 () (at Library/PackageCache/com.unity.ads@3.4.2/Runtime/Advertisement/Platform/Platform.cs:171)
    UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@3.4.2/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)


    I tried remove and install Advertisement package or downgrade it but doesn't works.
     
  2. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    This is not an error with the ads package. The ads package is calling the OnUnityAdsDidFinish callback after showing an ad and inside that your attempting to interact with a ParticleSystem that has been destroyed. Read the top line of your stacktrace which shows the bug in your code at: Assets/Scripts/UI/DiamondsAdded.cs:12
     
  3. antony_morar

    antony_morar

    Joined:
    Nov 7, 2017
    Posts:
    12
    I read the error but the particles are never destroyed and I can access them from any other script. All works fine until the same scene is reloaded
     
  4. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    My guess is that your AdMultiplyGems component isn't being properly destroyed as you reload the scene, giving you two versions: the new AdMultiplyGems in the reloaded scene, and a ghost version that is still registered with the Ads SDK.

    So when an ad finishes loading, the SDK actually sees two components that have been added as listeners. The problem is the old version of the component is trying to access things that have been destroyed - such as the ParticleSystem that was being used by the previous scene.

    Try adding this to any class that uses IUnityAdsListener:
    Code (CSharp):
    1.     private void OnDisable()
    2.     {
    3.         Advertisement.RemoveListener( this );
    4.     }
    When you reload the scene, this will tell the Ads SDK not to notify any old versions of your components.
     
    GerorgeCotuna, Beta4 and antony_morar like this.
  5. antony_morar

    antony_morar

    Joined:
    Nov 7, 2017
    Posts:
    12
    This worked for me! Thanks a lot
     
    kyle-unity likes this.