Search Unity

ads working but stop after reloading scene

Discussion in 'Unity Ads & User Acquisition' started by MickelDebs23, Jan 4, 2021.

  1. MickelDebs23

    MickelDebs23

    Joined:
    Dec 14, 2019
    Posts:
    1
    hey, I got this problem before but I got it fixed by just calling RemoveListener before reload the scene.
    This time its not working I don't know if there's anything wrong in my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5.  
    6. public class AdManager : MonoBehaviour, IUnityAdsListener
    7. {
    8.     public GameObject UndoMove;
    9.     public GameObject Player;
    10.  
    11.     private GameObject[] MovingPlatformsArray;
    12.     private Vector2[] MovingPlatformsPositonArray;
    13.  
    14.     [HideInInspector]public bool AdReady;
    15.  
    16.     public static AdManager instance;
    17.  
    18.     private string GooglePlay_ID = "ididid";
    19.     string rewardedId = "rewardedVideo";
    20.     private bool testMode = true;
    21.  
    22.     private void Start()
    23.     {
    24.         if (instance == null)
    25.         {
    26.             instance = this;
    27.         }
    28.         else {
    29.             Destroy(this);
    30.         }
    31.        
    32.        
    33.         Advertisement.AddListener(this);
    34.         Advertisement.Initialize(GooglePlay_ID, testMode);
    35.        
    36.     }
    37.  
    38.     public void ShowRewardedAd()
    39.     {
    40.         if (Advertisement.IsReady(rewardedId))
    41.         {
    42.             Advertisement.Show(rewardedId);
    43.         }
    44.  
    45.     }
    46.    
    47.     public void OnUnityAdsDidError(string message)
    48.     {
    49.        
    50.     }
    51.  
    52.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    53.     {
    54.         if (showResult == ShowResult.Finished)
    55.         {
    56.            
    57.             if (PlayerMovement.LastConnectedBody.gameObject.tag=="PlatformMove")
    58.             {
    59.                 MovingPlatformsArray = Player.GetComponent<PlayerMovement>().MovingPlatformsArray;
    60.                 MovingPlatformsPositonArray = Player.GetComponent<PlayerMovement>().MovingPlatformsPositonArray;
    61.                 int i = 0;
    62.                 foreach (GameObject MovingPlatform in MovingPlatformsArray)
    63.                 {
    64.                     MovingPlatform.transform.position = MovingPlatformsPositonArray[i];
    65.                     i++;
    66.                 }
    67.                 Player.GetComponent<PlayerMovement>().hj.autoConfigureConnectedAnchor = false;
    68.             }
    69.             Player.transform.position = PlayerMovement.LastMovePosition;
    70.             Player.GetComponent<PlayerMovement>().hj.connectedBody = PlayerMovement.LastConnectedBody;
    71.             Player.GetComponent<PlayerMovement>().hj.connectedAnchor = PlayerMovement.LastConnectedAnchor;
    72.             PlayerMovement.FirstMove = false;
    73.         }
    74.         else if (showResult == ShowResult.Skipped)
    75.         {
    76.  
    77.         }
    78.         else if (showResult == ShowResult.Failed)
    79.         {
    80.  
    81.         }
    82.     }
    83.  
    84.     public void OnUnityAdsDidStart(string placementId)
    85.     {
    86.         if (PlayerMovement.LastConnectedBody.gameObject.tag == "PlatformMove")
    87.         {
    88.             Player.GetComponent<PlayerMovement>().hj.autoConfigureConnectedAnchor = false;
    89.         }
    90.         else
    91.         {
    92.             Player.GetComponent<PlayerMovement>().hj.autoConfigureConnectedAnchor = true;
    93.         }
    94.     }
    95.  
    96.     public void OnUnityAdsReady(string placementId)
    97.     {
    98.  
    99.         if (placementId == rewardedId)
    100.         {
    101.             AdReady = true;
    102.         }
    103.         else
    104.         {
    105.             AdReady = false;
    106.         }
    107.     }
    108.  
    109.     public void DestroyInstance()
    110.     {
    111.         Advertisement.RemoveListener(this);
    112.        
    113.     }
    114.  
    115.  
    116.     private void Update()
    117.     {
    118.         if (PlayerMovement.FirstMove == true && AdReady == true)
    119.         {
    120.             UndoMove.SetActive(true);
    121.         }
    122.         else
    123.         {
    124.             UndoMove.SetActive(false);
    125.         }
    126.     }
    127.  
    128.  
    129. }
    130.  
    and here is when I reload my scene
    Code (CSharp):
    1. public void Restart()
    2.     {
    3.         AdManager.instance.DestroyInstance();
    4.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    5.     }
    I checked if my function wasn't getting called with a print but it seems to be working fine.
    Tried switching to unity monetization from the asset store but same problem occurs
    any help please?
     
  2. kyle-unity

    kyle-unity

    Unity Technologies

    Joined:
    Jan 6, 2020
    Posts:
    336
    Looks like you're not resetting the "instance" static variable to null, which means your Start method will always make the component destroy itself after the first scene.

    Try adding this to your DestroyInstance method:
    Code (CSharp):
    1. instance = null;
     
  3. aityomeike

    aityomeike

    Joined:
    Sep 17, 2021
    Posts:
    2
    I am having the same issue, how do i add this instance = null; and where. i am a newbie to this
    my own is rewarded ads
    This code keeps destroying my ads when the scene is reloaded

    _showAdButton.onClick.RemoveAllListeners();
    how do I get it not to destroy when a new scene reloads?
    This is for a rewarded ad the ad works fine on the first scene.
     
    raisa_z likes this.