• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IFixGames · Oct 25, 2021 at 06:38 PM · unity ads

Multiple Ads

Hi, I have several rewarded ads included in my app, one to double the player's offline income when the game starts and another to double the in-game income for a certain time. Now I have the problem that no matter which ad is viewed, I get the reward for both ads. My scripts look like this: using UnityEngine; using UnityEngine.UI; using UnityEngine.Advertisements;

 public class Ads : MonoBehaviour, IUnityAdsListener
 {
     public Clicker clicker;
 
 #if UNITY_IOS
     private string gameId = "4124812";
 #elif UNITY_ANDROID
     private string gameId = "4124813";
 #endif
 
     bool testMode = true;
     int Multiplier = 2;
 
     public float BoostTimer;
     public int AdLock = 4;
     public int PlayedAds;
     public bool MultiplierActive = false;
     public bool Done;
 
     public string myPlacementId = "rewardedVideo";
 
     public Button AdButton; 
     public GameObject AdMenuPanel;
 
     public Text TimerText;
     public Slider TimerSlider;
 
     private void Start()
     {
         Advertisement.Initialize(gameId, testMode);
         Advertisement.AddListener(this);
 
         AdButton.GetComponent<Button>();
         AdButton.interactable = Advertisement.IsReady(myPlacementId);
         if (AdButton) AdButton.onClick.AddListener(ShowRewardedVideo);
 
         BoostTimer = 0;
         TimerSlider.interactable = false;
         TimerSlider.maxValue = 3600F;
     }
 
     private void Update()
     {
         Check();
 
         int hours = Mathf.FloorToInt(BoostTimer / 3600F);
         int minutes = Mathf.FloorToInt((BoostTimer % 3600F) / 60);
         int seconds = Mathf.FloorToInt(BoostTimer % 60);
 
         string formattedTime = string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
 
         TimerText.text = formattedTime.ToString();
 
         TimerSlider.value = BoostTimer;
     }
 
     void AdFinished()
     {
         if(PlayedAds <= AdLock)
         {
             BoostTimer += 900;
             PlayedAds++;
         }
     }
 
     void Check()
     {
         if (PlayedAds == 1 && MultiplierActive == true && Done == false)
         {
             clicker.PerClick *= Multiplier;
             Done = true;
         }
         else if (PlayedAds == 0 && MultiplierActive == false && Done == true)
         {
             clicker.PerClick /= Multiplier;
             Done = false;
         }
 
         if (BoostTimer > 0)
         {
             BoostTimer -= Time.deltaTime;
             MultiplierActive = true;
         }
         else if (BoostTimer <= 0)
         {
             PlayedAds = 0;
             MultiplierActive = false;
             BoostTimer = 0;
         }
 
         if (PlayedAds == AdLock)
         {
             AdButton.interactable = false;
         }
         else if (PlayedAds <= AdLock)
         {
             AdButton.interactable = true;
         }
     }
 
     void ShowRewardedVideo()
     {
         Advertisement.Show(myPlacementId);
     }
 
     public void OnUnityAdsReady(string placementId)
     {
         if(placementId == myPlacementId)
         {
             AdButton.interactable = true;
         }
     }
     
     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
     {
         if (showResult == ShowResult.Finished)
         {
             AdFinished();
         }
         else if (showResult == ShowResult.Skipped)
         {
             //Kein Reward
         }
         else if (showResult == ShowResult.Failed)
         {
             Debug.LogWarning("The ad did not finish due to an error");
         }
     }
 
     public void OnUnityAdsDidError(string message)
     {
         //throw new System.NotImplementedException();
     }
 
     public void OnUnityAdsDidStart(string placementId)
     {
 
     }
 
     public void OpenAdMenu()
     {
         AdMenuPanel.SetActive(true);
     }
 
     public void CloseAdMenu()
     {
         AdMenuPanel.SetActive(false);
     }
 }

And here's the second one:

 using UnityEngine;
 using System;
 using UnityEngine.UI;
 using UnityEngine.Advertisements;
 
 public class OfflineIncome : MonoBehaviour, IUnityAdsListener
 {
     public Items OfflineItem;
     public Clicker clicker;
     public Save save;
     public Rps rps;
 
     public int MaxOfflineTime = 7200;
     public float CurrentOfflineTime;
     public float OfflineEarned;
     public float AwayFor;
     public float RewardedFor;
 
     public Text OfflineEarnedText;
     public Text AwayForText;
 
     public Button TakeButton;
     public Button DoubleButton;
 
     public GameObject OfflinePanel;
     public GameObject AdStatusPanel;
 
     public Button AcceptAdStatus;
     public Text AdStatus;
     public Text AdStatusText;
 
     DateTime currentTime;
     DateTime endTime;
 
     public double seconds;
 
     public float days;
     public float hours;
     public float minutes;
     public float secs;
 
 #if UNITY_IOS
     private string gameId = "4124812";
 #elif UNITY_ANDROID
     private string gameId = "4124813";
 #endif
 
     bool testMode = true;
     public string myPlacementId = "rewardedVideo";
 
     private void Start()
     {
         Advertisement.Initialize(gameId, testMode);
         Advertisement.AddListener(this);
 
         DoubleButton.GetComponent<Button>();
         DoubleButton.interactable = Advertisement.IsReady(myPlacementId);
         if (DoubleButton) DoubleButton.onClick.AddListener(ShowRewardedVideo);
     }
 
     void ShowRewardedVideo()
     {
         Advertisement.Show(myPlacementId);
     }
 
     public void OnUnityAdsReady(string placementId)
     {
         if (placementId == myPlacementId)
         {
             DoubleButton.interactable = true;
         }
     }
 
     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
     {
         if (showResult == ShowResult.Finished)
         {
             OfflineEarned *= 2;
             clicker.Radiation += OfflineEarned;
             clicker.TotalEarned += OfflineEarned;
             AdStatusPanel.SetActive(true);
             AdStatus.text = "Ad finished successfully";
             if (OfflineEarned <= 999)
             {
                 AdStatusText.text = OfflineEarned.ToString("F2") + " Radiation";
             }
             if (OfflineEarned >= 1000)
             {
                 AdStatusText.text = (OfflineEarned / 1000).ToString("F2") + "K" + " Radiation";
             }
             if (OfflineEarned >= 1000000)
             {
                 AdStatusText.text = (OfflineEarned / 1000000).ToString("F2") + "M" + " Radiation";
             }
             if (OfflineEarned >= 1000000000)
             {
                 AdStatusText.text = (OfflineEarned / 1000000000).ToString("F2") + "B" + " Radiation";
             }
         }
         else if (showResult == ShowResult.Skipped)
         {
             //Kein Reward
         }
         else if (showResult == ShowResult.Failed)
         {
             AdStatusPanel.SetActive(true);
             AdStatus.text = "Ad failed";
             AdStatusText.text = "The ad did not finish due to an error!";
         }
     }
 
     public void OnUnityAdsDidError(string message)
     {
 
     }
 
     public void OnUnityAdsDidStart(string placementId)
     {
 
     }
 
     public void Accept()
     {
         AdStatusPanel.SetActive(false);
     }
 
     public void Take()
     {
         clicker.Radiation += OfflineEarned;
         clicker.TotalEarned += OfflineEarned;
         OfflinePanel.SetActive(false);
     }
 
     public void Started()
     {
         //rps.CalculateRadiationPerSecond();
         OfflinePanel.SetActive(true);
         currentTime = System.DateTime.Now;
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
         endTime = DateTime.FromBinary(temp);
         TimeSpan difference = currentTime.Subtract(endTime);
         //string TimeText = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", difference.Days, difference.Hours, difference.Minutes, difference.Seconds);
 
         seconds = difference.TotalSeconds;
 
         days = Convert.ToSingle(difference.Days);
         hours = Convert.ToSingle(difference.Hours);
         minutes = Convert.ToSingle(difference.Minutes);
         secs = Convert.ToSingle(difference.Seconds);
 
         if (seconds >= MaxOfflineTime)
         {
             seconds = MaxOfflineTime;
             OfflineEarned = (MaxOfflineTime * rps.RadiationPerSecond) * OfflineItem.OfflineIncome;
         }
         else if (seconds < MaxOfflineTime)
         {
             OfflineEarned = ((float)seconds  * rps.RadiationPerSecond) * OfflineItem.OfflineIncome;
         }
 
         if(days > 0)
         {
             AwayForText.text = days + "d " + hours + "h " + minutes + "m " + secs + "s";
         }
         else if( hours > 0)
         {
             AwayForText.text = hours + "h " + minutes + "m " + secs + "s";
         }
         else if (minutes > 0)
         {
             AwayForText.text = minutes + "m " + secs + "s";
         }
         else if (secs> 0)
         {
             AwayForText.text = secs + "s";
         }
 
         if (OfflineEarned <= 999)
         {
             OfflineEarnedText.text = OfflineEarned.ToString("F2") + " Radiation";
         }
         if (OfflineEarned >= 1000)
         {
             OfflineEarnedText.text = (OfflineEarned / 1000).ToString("F2") + "K" + " Radiation";
         }
         if (OfflineEarned >= 1000000)
         {
             OfflineEarnedText.text = (OfflineEarned / 1000000).ToString("F2") + "M" + " Radiation";
         }
         if (OfflineEarned >= 1000000000)
         {
             OfflineEarnedText.text = (OfflineEarned / 1000000000).ToString("F2") + "B" + " Radiation";
         }
         //AwayForText.text = TimeText;
     }
 
     public void Double()
     {
         OfflinePanel.SetActive(false);
     }
 
     private void OnApplicationQuit()
     {
         PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
         Debug.Log("Saving this time: " + System.DateTime.Now);
     }
 
     private void OnApplicationPause(bool pause)
     {
         if (pause)
         {
             PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
             Debug.Log("Saving this time: " + System.DateTime.Now);
         }
     }
 }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Ad failing to load after being played several times 1 Answer

unity ads in tunisia 1 Answer

UnityAds Banner not showing up in production environment/build 10 Answers

Multiple unity Ads 0 Answers

Getting rewarded ads to work 'Unity ads' 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges