Unity ads Script gets destroyed on Scene Change / Reload

Hello, i hope someone can help me, sorry if there is already a thread with this but i didnt find one.

I have attached my ads script to every scene and as long as the scene does not get changed the ads do work but as soon a realod / scene change happens i get the following error:
(i think something is wrong with the Coroutine, i have tried it without time scale but it didnt work)

MissingReferenceException: The object of type ‘AdsScript’ 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.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) (at <23a7799da2e941b88c6db790c607d655>:0)
AdsScript.OnUnityAdsDidFinish (System.String placementId, UnityEngine.Advertisements.ShowResult showResult) (at Assets/Scripts/AdsScript.cs:54)
UnityEngine.Advertisements.Platform.Platform+<>c__DisplayClass41_0.b__0 () (at Library/PackageCache/com.unity.ads@3.4.9/Runtime/Advertisement/Platform/Platform.cs:171)
UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@3.4.9/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)

My Code:

void Awake()
{
 
    Physics2D.IgnoreLayerCollision (9, 11, false);
}
 
 
    void Start()
    {
    
        Advertisement.AddListener(this);
        Advertisement.Initialize(GooglePlay_ID, testMode);
    }

    // Update is called once per frame
  public void DisplayInterstitialAD(){
      Advertisement.Show();
  }
 
  public void DisplayVideoAD(){
      Advertisement.Show(myPlacementId);
      Physics2D.IgnoreLayerCollision (9, 11, true);
  }
    // Implement IUnityAdsListener interface methods:
    public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
        // Define conditional logic for each ad completion status:
    
        if (showResult == ShowResult.Finished)
        {
            if(PlayerPrefs.GetInt("Continue") >= 1)
            {
                Time.timeScale = 1;
                StartCoroutine(GetInvulnerable());
                PlayerPrefs.DeleteKey("Continue");

            }

            if(PlayerPrefs.GetInt("2D") >=1)
            {
                PlayerPrefs.SetInt("brushes", PlayerPrefs.GetInt("brushes") + 15);
                PlayerPrefs.DeleteKey("2D");

            }
             if(PlayerPrefs.GetInt("3D") >=1)
            {
                PlayerPrefs.SetInt("Money", PlayerPrefs.GetInt("Money") + 50);
                PlayerPrefs.DeleteKey("3D");

            }
     

        }
         else if (showResult == ShowResult.Skipped)
        {
        
            Debug.LogWarning ("No reward.");
        }
         else if (showResult == ShowResult.Failed)
         {
             Physics2D.IgnoreLayerCollision (9, 11, false);
            Debug.LogWarning ("The ad did not finish due to an error.");
        }
    }

    public void OnUnityAdsReady (string placementId) {
        // If the ready Placement is rewarded, show the ad:
        if (placementId == myPlacementId) {
         
        }
    }

    public void OnUnityAdsDidError (string message) {
        // Log the error.
    }

    public void OnUnityAdsDidStart (string placementId) {
        // Optional actions to take when the end-users triggers an ad.
    }

    IEnumerator GetInvulnerable()
    {
         Physics2D.IgnoreLayerCollision (9, 11, true);
         Rmenu2.SetActive(false);
        health.GiveHealth();
        yield return new WaitForSeconds (5);
        Physics2D.IgnoreLayerCollision (9, 11, false);
 
      
     
     

    }
    public void continiue()
    {
         PlayerPrefs.SetInt("Continue", PlayerPrefs.GetInt("Continue") + 1);
    }
    public void Coins2D()
    {
        PlayerPrefs.SetInt("2D", PlayerPrefs.GetInt("2D") + 1);
    }
    public void Coins3D()
    {
        PlayerPrefs.SetInt("3D", PlayerPrefs.GetInt("3D") + 1);
    }
    public void Play10()
    {
        PlayerPrefs.SetInt("Play", PlayerPrefs.GetInt("Play") + 1);
        if(PlayerPrefs.GetInt("Play") >= 10)
        {
            DisplayVideoAD();
            PlayerPrefs.DeleteKey("Play");

        }
    }
}

thank you very much

So the issue your having is that the OnUnityAdsFinished callback is trying to invoke the callback on the listener you passed it with the AddListener call in Start, but when you changed your scene the gameobject holding your monobehavior with that implementation was likely destroyed. You likely just need to remove the old listener when you no longer need it (for example when you change scenes), and then add a new listener in your new scene. You could also setup the gameobject to not be destroyed on scene transition.

it worked with removing the add listener on scene change, thank you very much

2 Likes

Can you explain me how did you managed remove Listener on scene change ? Please