I have this script where I want the ads to show every 5 rounds/losses. When I test the game the ads just don't show. They only show when I put 1 in the gamesToShowAd variable. I have tried multiple ways how to make ads play every 5 rounds/losses and none of them worked. GameManager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject gameOverCanvas;
public AdsManager ads;
int gameCount = 0;
int gamesToShowAd = 5;
private void Start()
{
Time.timeScale = 1;
ads.ShowBanner();
gameCount = 0;
}
public void GameOver()
{
gameOverCanvas.SetActive(true);
Time.timeScale = 0;
gameCount ; //increment game count
if (gameCount >= gamesToShowAd) // check if player played enough games to show ad
{
ads.PlayAd();
gameCount = 0; // reset counter
}
}
public void Replay()
{
SceneManager.LoadScene(0);
}
}
Maybe its something to do with the ads manager script? AdsManager:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class AdsManager : MonoBehaviour, IUnityAdsListener
{
#if UNITY_IOS
string gameId = "#######";
#else
string gameId = "#######";
#endif
Action onRewardedAdSuccess;
// Start is called before the first frame update
void Start()
{
Advertisement.Initialize(gameId);
Advertisement.AddListener(this);
ShowBanner();
}
public void PlayAd()
{
if(Advertisement.IsReady("Interstitial_Android"))
Advertisement.Show("Interstitial_Android");
}
public void PlayRewardedAd(Action onSuccess)
{
onRewardedAdSuccess = onSuccess;
if(Advertisement.IsReady("Rewarded_Android"))
{
Advertisement.Show("Rewarded_Android");
}
else
{
Debug.Log("Rewarded ad is not ready!");
}
}
public void ShowBanner()
{
if (Advertisement.IsReady("Banner_Android"))
{
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show("Banner_Android");
}
else
{
StartCoroutine(RepeatShowBanner());
}
}
public void HideBanner()
{
Advertisement.Banner.Hide();
}
IEnumerator RepeatShowBanner()
{
yield return new WaitForSeconds(1);
ShowBanner();
}
public void OnUnityAdsReady(string placementId)
{
Debug.Log("ADS ARE READY!");
}
public void OnUnityAdsDidError(string message)
{
Debug.Log("ERROR: " message);
}
public void OnUnityAdsDidStart(string placementId)
{
Debug.Log("VIDEO STARTED!");
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
if (placementId == "Rewarded_Android" && showResult == ShowResult.Finished)
{
onRewardedAdSuccess.Invoke();
}
}
}
Any help is appreciated!
CodePudding user response:
Instead of 5 rounds, why don't you make it 20% chance of playing every round? This is more efficient than your way.
But your issue seems to be here. (Unity calls Start() and Awake() every time the scene that the object is from is called)
Take a look at this.
private void Start(){
Time.timeScale = 1;
ads.ShowBanner();
gameCount = 0;
//It sets to 0 every time your scene reloads
//Try creating this object at your main menu or loading a empty scene before hitting play on menu
}
Or if you want that percentage to show ads, create this:
[Range(0, 10)]
public int chanceToShowAds;
private int currentChance;
public void GameOver(){
currentChance = Random.Range(0, 10);
if (currentChance >= chanceToShowAds)
ads.PlayAd();
}