Search Unity

Trying to hide "Remove Ads" button whilst ads are switched off but not staying hidden

Discussion in '2D' started by Dommo1, Jul 27, 2019.

  1. Dommo1

    Dommo1

    Joined:
    Oct 28, 2018
    Posts:
    125
    Hi!

    So I have this script that removes ads for 10 minutes if the player has watched a rewarded ad:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using System;
    6.  
    7. public class AdManager : MonoBehaviour
    8. {
    9.     private static int loadCount = 0;
    10.     private static bool noAds = false;
    11.     private static DateTime noAdsTime = new DateTime();
    12.  
    13.     void Start()
    14.     {
    15.         DateTime currentTime = DateTime.Now;
    16.  
    17.         if (noAdsTime != new DateTime())
    18.             noAds = DateTime.Compare(currentTime, noAdsTime) < 0 ? true : false; //DateTime.Compare returns less than zero if currentTime is earlier than noAdsTime.
    19.  
    20.         if (loadCount % 5 == 0 && noAds == false)  //  show ad every 5th time
    21.         {
    22.             ShowAd();
    23.         }
    24.         loadCount++;
    25.     }
    26.  
    27.  
    28.     public void ActivateNoAds(int minutes)
    29.     {
    30.         Advertisement.Show();
    31.         DateTime currentTime = DateTime.Now;
    32.         noAdsTime = currentTime.AddMinutes(minutes);
    33.  
    34.     }
    35.     public void ShowAd()
    36.     {
    37.         if (Advertisement.IsReady())
    38.         {
    39.             Advertisement.Show();
    40.         }
    41.     }
    42. }
    That works fine. And now I am trying to hide the "Remove Ads" button during the 10 mins of no ads. I have tried to copy the structure of the above script to hide the button for 10 mins too... And I can get it to hide but the next time the player lands on game over the button appears again (but the ads are still off). I have tried its own script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class AdsRemoveButton : MonoBehaviour
    7. {
    8.  
    9.     private static bool noButton = false;
    10.     private static DateTime noButtonTime = new DateTime();
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         DateTime currentTime = DateTime.Now;
    16.  
    17.         if (noButtonTime != new DateTime())
    18.             noButton = DateTime.Compare(currentTime, noButtonTime) < 0 ? true : false; //DateTime.Compare returns less than zero if currentTime is earlier than noAdsTime.
    19.     }
    20.  
    21.     public void ActivateNoButton(int minutes)
    22.     {
    23.  
    24.         DateTime currentTime = DateTime.Now;
    25.         noButtonTime = currentTime.AddMinutes(minutes);
    26.         GameObject.Find("Remove Ads Button").transform.localScale = new Vector3(0, 0, 0); // Hide button
    27.  
    28.  
    29.     }
    30. }
    And a combined script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using System;
    6.  
    7. public class AdManager : MonoBehaviour
    8. {
    9.     private static int loadCount = 0;
    10.     private static bool noAds = false;
    11.     private static DateTime noAdsTime = new DateTime();
    12.     private static bool noButton = false;
    13.     private static DateTime noButtonTime = new DateTime();
    14.  
    15.     void Start()
    16.     {
    17.         DateTime currentTime = DateTime.Now;
    18.  
    19.         if (noAdsTime != new DateTime())
    20.             noAds = DateTime.Compare(currentTime, noAdsTime) < 0 ? true : false; //DateTime.Compare returns less than zero if currentTime is earlier than noAdsTime.
    21.  
    22.         if (noButtonTime != new DateTime())
    23.             noButton = DateTime.Compare(currentTime, noButtonTime) < 0 ? true : false; //DateTime.Compare returns less than zero if currentTime is earlier than noAdsTime.
    24.  
    25.         if (loadCount % 5 == 0 && noAds == false)  //  show ad every 5th time
    26.         {
    27.             ShowAd();
    28.         }
    29.         loadCount++;
    30.     }
    31.  
    32.  
    33.     public void ActivateNoAds(int minutes)
    34.     {
    35.         Advertisement.Show();
    36.         DateTime currentTime = DateTime.Now;
    37.         noAdsTime = currentTime.AddMinutes(minutes);
    38.      
    39.  
    40.     }
    41.  
    42.     public void ActivateNoButton(int minutes)
    43.     {
    44.      
    45.         DateTime currentTime = DateTime.Now;
    46.         noButtonTime = currentTime.AddMinutes(minutes);
    47.         GameObject.Find("Remove Ads Button").transform.localScale = new Vector3(0, 0, 0); // Hide button
    48.  
    49.  
    50.     }
    51.  
    52.     public void ShowAd()
    53.     {
    54.         if (Advertisement.IsReady())
    55.         {
    56.             Advertisement.Show();
    57.         }
    58.     }
    59. }

    Please forgive my probably dumb mistake. I am very much a beginner.

    I am assigning these to the button's On Click and getting no errors so I don't know why it is not working? The

    Any help much appreciated!

    Thanks!
     
    Last edited: Jul 27, 2019