Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

458
Vistas
Unity - Advertisment called multiple times

I'm having this problem using Unity's Advertisment. Specifically after watching the video and clicking the X button to close the video, I should give the prize to the player (go to the next level). The problem is that the OnUnityAdsDidFinish function calls if (showResult == ShowResult.Finished) multiple times. What am I doing wrong? how do I call the FindObjectOfType () .LoadNextLevel () function once; ? Thank you in advance

public class UnityAdsInterstitial : MonoBehaviour, IUnityAdsListener
{
private string gameID = "******";
//nome scelto nella DashBoard di Unity
private string interstitialID = "interstitial";

private string myPlacementId = "rewardedVideo";

public int randomHighValue = 30;

private bool TestMode = true;

private bool adsClosed = false;

public Button _button;

private void Awake()
{
    _button = GetComponent<Button>();
}

void Start()
{
    Debug.Log("Ads  start");
    _button = GameObject.Find("StartAds").GetComponent<Button>();

    _button.interactable = Advertisement.IsReady(myPlacementId);

    if (_button) _button.onClick.AddListener(ShowRewardedVideo);

    Advertisement.Initialize(gameID, TestMode);
    Advertisement.AddListener(this);
    if (adsClosed)
    {
        adsClosed = false;
    }
    
}

public void ShowInterstitial()
{
    if (Advertisement.IsReady(interstitialID) )
    {
        Advertisement.Show(interstitialID);
    }
}

public void ShowRewardedVideo()
{
    if (Advertisement.IsReady(myPlacementId))
    {
        Debug.Log("Rewarded video is Ready");
        Advertisement.Show(myPlacementId);
    }
    else
    {
        Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
    }
}

public void HideBanner()
{
    Advertisement.Banner.Hide();
}

public void OnUnityAdsReady(string placementdID)
{
    if (placementdID == interstitialID)
    {
        Debug.Log("InterstitialIsReady");
    }

    if (placementdID == myPlacementId)
    {
        Debug.Log("RewardedIsReady");
        _button.interactable = true;
    }

}

public void OnUnityAdsDidFinish(string placementdID, ShowResult showResult)
{
    if (showResult == ShowResult.Finished)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }
    else if (showResult == ShowResult.Skipped)
    {
        // Do not reward the user for skipping the ad.
    }
    else if (showResult == ShowResult.Failed)
    {
        Debug.LogWarning("The ad did not finish due to an error.");
    }
}

public void OnUnityAdsDidError(string message)
{

    Debug.Log("OnUnityAdsDidError");
}
public void OnUnityAdsDidStart(string message)
{

    Debug.Log("OnUnityAdsDidStart");
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I would start my investigation by checking the placement id (in case there are more placements)

  1. Checking that the callback is for the proper placement id
    if (showResult == ShowResult.Finished && placementId == myPlacementId)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }

The second would be that I only have one active instance of UnityAdsInterstitial. You can check this in debug mode by the reference of the object. If more than one instance starts from somewhere else in your code, then you should just limit to one.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda