Search Unity

Bug The second time ads play my iOS game crashes!

Discussion in 'Scripting' started by Sammyueru1, May 7, 2021.

  1. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Alright so if I watch one extra life (or continue) ad the iOS game is fine but if I watch a second the game will crash on iOS and in Unity I'll get this error:
    Code (CSharp):
    1.  
    2. MissingReferenceException: The object of type 'AdContinue' has been destroyed but you are still trying to access it.
    3. Your script should either check if it is null or you should not destroy the object.
    4. UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/MonoBehaviour.bindings.cs:88)
    5. AdContinue.GetReward () (at Assets/Scripts/AdContinue.cs:48)
    6. UnityMobileAds.OnUnityAdsDidFinish (System.String placementID, UnityEngine.Advertisements.ShowResult showResult) (at Assets/Scripts/UnityMobileAds.cs:57)
    7. UnityEngine.Advertisements.Platform.Platform+<>c__DisplayClass42_0.<UnityAdsDidFinish>b__0 () (at Library/PackageCache/com.unity.ads@3.7.1/Runtime/Advertisement/Platform/Platform.cs:176)
    8. UnityEngine.Advertisements.Utilities.CoroutineExecutor.Update () (at Library/PackageCache/com.unity.ads@3.7.1/Runtime/Advertisement/Utilities/CoroutineExecutor.cs:17)
    9.  
    10.  
    And I want to put this game on the app store and after I get this fixed I think I will but I can't figure out how to fix it anyways if you can help thanks.
    Here's the other scripts:

    UnityMobileAds Script:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4. using UnityEngine.UI;
    5.  
    6. public class UnityMobileAds : MonoBehaviour, IUnityAdsListener
    7. {
    8.     private string gameID = "3875448";
    9.     private string bannerID = "AdBanner";
    10.     private string rewardedContinueVideoID = "RewardAdContinue";
    11.     public bool TestMode;
    12.     public AdContinue adContinueScript;
    13.  
    14.     void Start()
    15.     {
    16.         Advertisement.Initialize(gameID, TestMode);
    17.         Advertisement.AddListener(this);
    18.     }
    19.  
    20.     public void HideBanner()
    21.     {
    22.         Advertisement.Banner.Hide();
    23.     }
    24.  
    25.     public void OnUnityAdsReady(string placementID)
    26.     {
    27.         if (placementID == bannerID)
    28.         {
    29.             Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
    30.             Advertisement.Banner.Show(bannerID);
    31.         }
    32.     }
    33.  
    34.  
    35.     public void OnUnityAdsDidError(string message)
    36.     {
    37.        
    38.     }
    39.  
    40.     public void OnUnityAdsDidStart(string placementID)
    41.     {
    42.  
    43.     }
    44.  
    45.     public void ShowContinueRewardVideo()
    46.     {
    47.         Advertisement.Show(rewardedContinueVideoID);
    48.     }
    49.  
    50.  
    51.     public void OnUnityAdsDidFinish(string placementID, ShowResult showResult)
    52.     {
    53.         if (placementID == rewardedContinueVideoID)
    54.         {
    55.             if(showResult == ShowResult.Finished)
    56.             {
    57.                 adContinueScript.GetReward();
    58.             }
    59.             else if (showResult == ShowResult.Skipped)
    60.             {
    61.  
    62.             }
    63.             else if (showResult == ShowResult.Failed)
    64.             {
    65.  
    66.             }
    67.         }
    68.     }
    69.  
    70. }
    AdContinue Script:
    Code (CSharp):
    1. using UnityEngine.Events;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5. using System;
    6. using System.Collections.Generic;
    7. using System.Collections;
    8. using SgLib;
    9.  
    10. public class AdContinue : MonoBehaviour
    11. {
    12.     public GameManager gameManager;
    13.     public ScoreManager ScoreManager;
    14.     public Text score;
    15.     public GameObject ExtraLifeAdButton;
    16.     public GameObject buttons;
    17.     public string scoreBeforeLoss;
    18.     public int scoreBeforeLossInt;
    19.     public UnityMobileAds unityMobileAds;
    20.  
    21.  
    22.    
    23.  
    24.     void Update()
    25.     {
    26.         if (!gameManager.gameOver)
    27.         {
    28.             scoreBeforeLoss = score.text;
    29.         }
    30.         int.TryParse(scoreBeforeLoss, out scoreBeforeLossInt);
    31.     }
    32.  
    33.    
    34.     public void ButtonAdContinue()
    35.     {
    36.         unityMobileAds.ShowContinueRewardVideo();
    37.     }
    38.  
    39.    
    40.     public void GetReward()
    41.     {
    42.         //buttons.SetActive(false);
    43.    
    44.         //score.gameObject.SetActive(true);
    45.  
    46.         //gameManager.StartGame();
    47.         //gameManager.CreateBall();
    48.         StartCoroutine(Continue());
    49.     }
    50.  
    51.     IEnumerator Continue()
    52.     {
    53.         yield return new WaitForSeconds(0.2f);
    54.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    55.         score.text = scoreBeforeLoss.ToString();
    56.         ScoreManager.Instance.Score2 = scoreBeforeLossInt;
    57.         ScoreManager.SetAdContinueScore();
    58.     }
    59. }
    Thanks in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Top script... see how you add yourself as a listener on line 17?

    When you get destroyed (eg when the scene changes) you need to un-add yourself. This might do it:

    Code (csharp):
    1. void OnDisable()
    2. {
    3.   if (Advertisement != null)
    4.   {
    5.     Advertisement.RemoveListener(this);
    6.   }
    7. }
    Alternately mark the entire thing as DontDestroyOnLoad() and it won't go away on scene changes, but you MUST MAKE SURE you don't add more of them with each additional scene.
     
    Sammyueru1 likes this.
  3. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Thanks I'll try that
     
  4. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Alright so when I try that I get this error:
    Code (CSharp):
    1. Assets/Scripts/UnityMobileAds.cs(74,6): error CS0116: A namespace cannot directly contain members such as fields or methods
     
  5. Are you sure you wrote
    Advertisement.RemoveListener(this);
    and not
    Advertisements.RemoveListener(this);
    ?
    Could you share your OnDisable method where the error appears?
     
  6. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    FIXED:
    I had to do something like this
    Code (CSharp):
    1. void OnDestroy()
    2. {
    3.    Advertisement.RemoveListener(this);
    4. }
     
    Kurt-Dekker likes this.
  7. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Sorry I fixed it by doing OnDestroy()
    It actually made things more simple too.
     
    Kurt-Dekker likes this.