Search Unity

Unity ads myPlacementID

Discussion in 'Getting Started' started by IamDrJanItor, Sep 7, 2019.

  1. IamDrJanItor


    Sep 7, 2019
    Hi! I'm following my first Unity tutorial and i'm at the end part where i'm supposed to implement ads. I think the tutorial might be a few years old because the the code he is using doesn't look at all like the current code in the implementation guide at
    I'm using the rewarded video section:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    4. public class RewardedAdsScript : MonoBehaviour, IUnityAdsListener {
    6.     string gameId = "1234567";
    7.     myPlacementId = “rewardedVideo”;
    8.     bool testMode = true;
    10.     // Initialize the Ads listener and service:
    11.     void Start () {
    12.         Advertisement.AddListener (this);
    13.         Advertisement.Initialize (gameId, testMode);
    14.     }
    16.     // Implement IUnityAdsListener interface methods:
    17.     public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
    18.         // Define conditional logic for each ad completion status:
    19.         if (showResult == ShowResult.Finished) {
    20.             // Reward the user for watching the ad to completion.
    21.         } else if (showResult == ShowResult.Skipped) {
    22.             // Do not reward the user for skipping the ad.
    23.         } else if (showResult == ShowResult.Failed) {
    24.             Debug.LogWarning (“The ad did not finish due to an error.);
    25.         }
    26.     }
    28.     public void OnUnityAdsReady (string placementId) {
    29.         // If the ready Placement is rewarded, show the ad:
    30.         if (placementId == myPlacementId) {
    31.             Advertisement.Show (myPlacementId);
    32.         }
    33.     }
    35.     public void OnUnityAdsDidError (string message) {
    36.         // Log the error.
    37.     }
    39.     public void OnUnityAdsDidStart (string placementId) {
    40.         // Optional actions to take when the end-users triggers an ad.
    41.     }
    42. }
    So i'm trying to follow this guide myself and i'm running in to some issues. I have an ad button that you are supposed to be able to press only once per game. If the button is pressed you get one extra life. Next time you die you have to restart the game.

    So i die once and i'm able to press the ad-button, the "This screen would be your ad unit" screen i shown, i continue the game until i die another time and have to restart. So far every thing works as intended. But now if i die after the game has restarted, i press the ad-button, i get a missing reference exeption. This is the full message:

    MissingReferenceException: The object of type 'GameObject' 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.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/GameObject.bindings.cs:28)
    App_Initialize.OnUnityAdsDidFinish (System.String placementId, UnityEngine.Advertisements.ShowResult showResult) (at Assets/Scripts/App_Initialize.cs:95)
    UnityEngine.Advertisements.Platform+<>c__DisplayClass31_1.<Initialize>b__2 (UnityEngine.Advertisements.CallbackExecutor execute) (at Library/PackageCache/
    UnityEngine.Advertisements.CallbackExecutor.Update () (at Library/PackageCache/

    The myPlacementId variable in the example code has no datatype. I made it a public string and other than that i really don't know what the problem could be. Any help would be very much appreciated!
  2. SoloOutlaw


    Feb 29, 2016
    This is exactly the same error I have.

    When I reload a scene I get a missing reference error.

    I don't need a Singleton because I am restarting as new.

    I do a null test on the variable outside the listener functions and the variable is loaded.

    Once its inside the function it doesn't exist even though you pause the game and its there.
  3. SoloOutlaw


    Feb 29, 2016
    see if this works... working for me.

    I think I have found a solution.

    void OnDestroy()


    I think the active listener stays in memory next time round.
    Kennai likes this.
  4. Kennai


    Nov 1, 2018
    And dont forget important note:
    OnDestroy will only be called on game objects that have previously been active.
    SoloOutlaw likes this.