Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[ SOLVED ] Persistent Listeners Problem on Scene Reload

Discussion in 'Scripting' started by Deleted User, Feb 29, 2020.

  1. Deleted User

    Deleted User

    Guest

    I've set up ads in my game, on an empty GameObject, I have a script AdManager.cs, and in the start method, I have :
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         Advertisement.AddListener(this);
    4.         InitializeAdvertisement();
    5.     }
    This is fine, first time round, any subsequent scene reload, seems to leave the previous Listener present, and obviously adds a new one to the scene, how can I check for and remove any previous listeners I have added using this method ?
     
  2. Deleted User

    Deleted User

    Guest

    So, after some 'painstaking' research ( documentation and forums posts, web searches weren't really clear on this ) I added :
    Code (CSharp):
    1. private void OnDisable()
    2.     {
    3.         Advertisement.RemoveListener(this);
    4.     }
    Then when I end my game, just before I reload the main scene, I simply deactivate my AdManager GameObject, which calls the OnDisable function and removes the current Listener.

    Leaving this here should anyone else be having the same problem.
     
    Last edited by a moderator: Dec 30, 2020
  3. AdamL_CN

    AdamL_CN

    Joined:
    Jul 26, 2020
    Posts:
    1
    It helps me a lot, thanks!