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!