Search Unity

Unity ads myPlacementID

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

  1. IamDrJanItor

    IamDrJanItor

    Joined:
    Sep 7, 2019
    Posts:
    1
    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 https://unityads.unity3d.com/help/unity/integration-guide-unity#basic-implementation
    I'm using the rewarded video section:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class RewardedAdsScript : MonoBehaviour, IUnityAdsListener {
    5.  
    6.     string gameId = "1234567";
    7.     myPlacementId = “rewardedVideo”;
    8.     bool testMode = true;
    9.  
    10.     // Initialize the Ads listener and service:
    11.     void Start () {
    12.         Advertisement.AddListener (this);
    13.         Advertisement.Initialize (gameId, testMode);
    14.     }
    15.  
    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.     }
    27.  
    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.     }
    34.  
    35.     public void OnUnityAdsDidError (string message) {
    36.         // Log the error.
    37.     }
    38.  
    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/com.unity.ads@3.2.0/Runtime/Advertisement/Platform.cs:128)
    UnityEngine.Advertisements.CallbackExecutor.Update () (at Library/PackageCache/com.unity.ads@3.2.0/Runtime/Advertisement/CallbackExecutor.cs:25)

    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

    SoloOutlaw

    Joined:
    Feb 29, 2016
    Posts:
    15
    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

    SoloOutlaw

    Joined:
    Feb 29, 2016
    Posts:
    15
    see if this works... working for me.

    I think I have found a solution.

    void OnDestroy()

    Debug.Log("DestroyAdController");
    myButton.onClick.RemoveListener(ShowRewardedVideo);
    Advertisement.RemoveListener(this);

    I think the active listener stays in memory next time round.
     
    fonexdx, arvindprasad1289 and Kennai like this.
  4. Kennai

    Kennai

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

    arvindprasad1289

    Joined:
    Mar 28, 2020
    Posts:
    3
    Thanks a ton mate. This works like charm. you saved me from many sleepless nights lol
     
  6. Udit_Warikoo

    Udit_Warikoo

    Joined:
    Sep 4, 2017
    Posts:
    5

    Could You elaborate on this ? I'm not understanding where I'm supposed to put this
     
  7. kocakberatfurkan

    kocakberatfurkan

    Joined:
    Sep 14, 2019
    Posts:
    5
    Hello, I cant use RemoveListener function. Is it no longer in use ?
     
    JudyBootyStudios likes this.