Search Unity

Unity Ad only plays the first time

Discussion in 'Unity Ads & User Acquisition' started by TruthVoyager, Oct 23, 2020.

  1. TruthVoyager

    TruthVoyager

    Joined:
    Jun 27, 2016
    Posts:
    4
    Alright, so here is what happens.

    When the player finishes a match, they are sent to a scene that checks if there is an ad ready via the following script. If there is an available Ad it sends the player to the ads scene to watch the ad via the script after the following, otherwise the user is sent to the landing screen which redirects them to the main menu.

    Theoretically the player should see an ad every time they finish a match unless one of the following is true: they have purchased a map, they are playing offline, or there is no available ad.

    In situations where the player is both online and has not made a purchase, the ad will play fine after the first match, but no other ads will play after any other match unless the app is closed and reopened. The reason they don't play is because no further ads get readied. This is the problem. There should be an ad after every match.

    I'm not sure if I am allowed to direct you to the game but, If you want to see this in action, the game is ZomPow2 and its on both the app store and google play.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class CheckForAds : MonoBehaviour
    6. {
    7.     bool ran;
    8.     // Start is called before the first frame update
    9.     void Update()
    10.     {
    11.         if(GameObject.Find("FloatingManager") && ran == false)
    12.         {
    13.             if (GameObject.Find("FloatingManager").GetComponent<SaveInfo>())
    14.             {
    15.                 ran = true;
    16.                 StartCoroutine(Adwait());
    17.             }
    18.         }
    19.     }
    20.  
    21.  
    22.     public IEnumerator Adwait()
    23.     {
    24.         yield return new WaitForSeconds(.5f);
    25.         if (GameObject.Find("FloatingManager").GetComponent<SaveInfo>().VirtualGoods != null)
    26.         {
    27.             if (!GameObject.Find("FloatingManager").GetComponent<SaveInfo>().VirtualGoods.Contains("AD_FREE_PRODUCT"))
    28.             {
    29.                 if (GameSparks.Core.GS.Authenticated)
    30.                 {
    31.                     Debug.Log("On to ads...");
    32.                     SceneManager.LoadScene("Ads");
    33.                 }
    34.                 else
    35.                 {
    36.                     Debug.Log("Ads offline...");
    37.                     Destroy(GameObject.Find("FloatingCanvas"));
    38.                     Destroy(GameObject.Find("GameSparks"));
    39.                     SceneManager.LoadScene("LandingScreen");
    40.                 }
    41.             }
    42.             else
    43.             {
    44.                 Debug.Log("Have ad free...");
    45.                 Destroy(GameObject.Find("FloatingCanvas"));
    46.                 Destroy(GameObject.Find("GameSparks"));
    47.                 //yield return new WaitForSeconds(.5f);
    48.                 if (GameSparks.Core.GS.Authenticated)
    49.                 {
    50.                     Debug.Log("Yay online and no ads...");
    51.                     SceneManager.LoadScene("LandingScreen");
    52.                 }
    53.                 else
    54.                 {
    55.                     Debug.Log("Offline but hey no ads...");
    56.                     SceneManager.LoadScene("LandingScreen");
    57.                 }
    58.             }
    59.         }
    60.         else
    61.         {
    62.             Debug.Log("Have ad free...");
    63.             Destroy(GameObject.Find("FloatingCanvas"));
    64.             Destroy(GameObject.Find("GameSparks"));
    65.             //yield return new WaitForSeconds(.5f);
    66.             if (GameSparks.Core.GS.Authenticated)
    67.             {
    68.                 Debug.Log("Yay online and no ads...");
    69.                 SceneManager.LoadScene("LandingScreen");
    70.             }
    71.             else
    72.             {
    73.                 Debug.Log("Offline but hey no ads...");
    74.                 SceneManager.LoadScene("LandingScreen");
    75.             }  
    76.         }
    77.     }
    78. }
    Here is the code that plays the Ad:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener
    6. {
    7.     public string myPlacementId = "rewardedVideo";
    8.     bool adFinished;
    9.     bool adLoaded;
    10.     bool triedManualLoad;
    11.     string otherplacmentId;
    12.     float loadingAdTimer = 1;
    13.     private string gameId;
    14.     void Start()
    15.     {
    16.         if(Application.platform == RuntimePlatform.IPhonePlayer)
    17.             gameId = "3633307";
    18.         else
    19.             gameId = "3633306";
    20.         // Set interactivity to be dependent on the Placement’s status:
    21.         //if (Advertisement.IsReady(myPlacementId))
    22.         //{
    23.         //ShowRewardedVideo();
    24.  
    25.         // Initialize the Ads listener and service:
    26.         Debug.Log("hit: " + adLoaded + "::" + gameId);
    27.             Advertisement.AddListener(this);
    28.             Advertisement.Initialize(gameId, true);
    29.         //}
    30.     }
    31.  
    32.     private void Update()
    33.     {
    34.         if (adLoaded == false)
    35.         {
    36.             TimerManager(ref loadingAdTimer);
    37.             if (loadingAdTimer <= 0 && !adLoaded && triedManualLoad == false)
    38.             {
    39.                 loadingAdTimer = 4;
    40.                 Advertisement.Load(myPlacementId);
    41.                 triedManualLoad = true;
    42.             }
    43.             else if (loadingAdTimer <= 0 && !adLoaded)
    44.             {
    45.                 Debug.Log("Error: ad failed to load...");
    46.                 Destroy(GameObject.Find("FloatingCanvas"));
    47.                 Destroy(GameObject.Find("GameSparks"));
    48.                 SceneManager.LoadScene("LandingScreen");
    49.             }
    50.         }
    51.  
    52.     }
    53.  
    54.     // Implement a function for showing a rewarded video ad:
    55.     void ShowRewardedVideo()
    56.     {
    57.         Debug.Log("hit3");
    58.         Advertisement.Show(otherplacmentId);
    59.     }
    60.  
    61.     // Implement IUnityAdsListener interface methods:
    62.     public void OnUnityAdsReady(string placementId)
    63.     {
    64.         Debug.Log("hit2 " + "ids: " + placementId + "/" + myPlacementId + " adLoaded: " + adLoaded);
    65.         // If the ready Placement is rewarded, activate the button:
    66.         if (!adLoaded)
    67.         {
    68.             otherplacmentId = placementId;
    69.             adLoaded = true;
    70.             ShowRewardedVideo();
    71.  
    72.             // Initialize the Ads listener and service:
    73.             Advertisement.AddListener(this);
    74.             Advertisement.Initialize(gameId, true);
    75.         }
    76.     }
    77.  
    78. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    79.     {
    80.         adFinished = true;
    81.         // Define conditional logic for each ad completion status:
    82.         if (showResult == ShowResult.Finished)
    83.         {
    84.             Debug.Log("fin");
    85.             Destroy(GameObject.Find("FloatingCanvas"));
    86.             Destroy(GameObject.Find("GameSparks"));
    87.             SceneManager.LoadScene("LandingScreen");
    88.         // Reward the user for watching the ad to completion.
    89.         }
    90.         else if (showResult == ShowResult.Skipped)
    91.         {
    92.             Debug.Log("skip");
    93.             Destroy(GameObject.Find("FloatingCanvas"));
    94.             Destroy(GameObject.Find("GameSparks"));
    95.             SceneManager.LoadScene("LandingScreen");
    96.         // Do not reward the user for skipping the ad.
    97.         }
    98.         else if (showResult == ShowResult.Failed)
    99.         {
    100.             Debug.Log("fail");
    101.             Destroy(GameObject.Find("FloatingCanvas"));
    102.             Destroy(GameObject.Find("GameSparks"));
    103.             SceneManager.LoadScene("LandingScreen");
    104.         }
    105.     }
    106.  
    107.     public void OnUnityAdsDidError(string message)
    108.     {
    109.         Debug.LogError(message);
    110.         Destroy(GameObject.Find("FloatingCanvas"));
    111.         Destroy(GameObject.Find("GameSparks"));
    112.         SceneManager.LoadScene("LandingScreen");
    113.         // Log the error.
    114.     }
    115.  
    116.     public void OnUnityAdsDidStart(string placementId)
    117.     {
    118.         // Optional actions to take when the end-users triggers an ad.
    119.     }
    120.  
    121.     //private void OnDestroy()
    122.     //{
    123.     //    if (!adFinished)
    124.     //    {
    125.     //        GameObject.Find("FloatingManager").GetComponent<SaveInfo>().discardScore();
    126.     //    }
    127.     //}
    128.  
    129.     private void TimerManager(ref float timer)
    130.     {
    131.         if (timer > 0)
    132.         {
    133.             timer -= Time.deltaTime;
    134.         }
    135.     }
    136. }
     
  2. pokelocos

    pokelocos

    Joined:
    Nov 9, 2015
    Posts:
    54
    Same problem, sorry for reviving this thread.
     
  3. SamOYUnity3D

    SamOYUnity3D

    Unity Technologies

    Joined:
    May 12, 2019
    Posts:
    626
    After showing the first ad, do you call the Load method to load the next ad? Are there any device logs you can provide?
     
  4. pokelocos

    pokelocos

    Joined:
    Nov 9, 2015
    Posts:
    54
    The problem we had has already been solved; it was that we were calling the "Load" function incorrectly at times when it didn't correspond.
     
    SamOYUnity3D likes this.