ADS WORK IN EDITOR BUT NOT AFTER BUILDING?

When in the Unity editor, test ads (Unity Ads) run fine. However, after publishing (I had assumed all was well), I discovered no ads were playing, causing players to get stuck in the preloading scene (I built a “preloading” scene before the first level/scene where I placed a script that runs the ad. When the ad is done, the first scene is loaded).

However, it seems the player was getting stuck in the preloading scene because no ad was played. I patched an update to the ad script, which loads the first scene in the case the ad fails. However, the problem remains that no ads play, despite working in the editor.

I am including the script I am using to run the ads in the preloading scene. I am unsure what to do as I am uncertain whether I am doing something wrong, if the Unity Ads servers are screwing up, or something else.

Here is the script being used for playing the ad, in it’s entirety:

          using System.Collections;
          using System.Collections.Generic;
          using UnityEngine;
          using UnityEngine.Advertisements;
          using UnityEngine.SceneManagement;

          public class PlayAd : MonoBehaviour
     {
          public static PlayAd Instance { set; get; }
public void Start()
{
    Instance = this;
    DontDestroyOnLoad(gameObject);
    Advertisement.Initialize("1370212", true);
    var showOptions = new ShowOptions
    {
        resultCallback = HandleShowResult
    };
    if (Advertisement.IsReady())
    {
        Advertisement.Show();
    }

}
public string LoadLevel
{
    get
    {
        return loadLevel;
    }

    set
    {
        loadLevel = value;
    }
}

[SerializeField] private string loadLevel;
private void HandleShowResult(ShowResult result)
{
    switch (result)
    {
        case ShowResult.Finished:

            LevelLoad();
            break;

        case ShowResult.Skipped:
            break;

        case ShowResult.Failed:

            break;
    }
}
public void LevelLoad()
{
    SceneManager.LoadScene(LoadLevel);

}


}

The answer was apparently that the ad was trying to load before it was initialized. As @aditya suggested, I used Advertisement.isSupported first, then, importantly, Advertisement.isInitialized. This caused the ads to play continuously, but all I had to do was make sure they were in the right place as well as StartCoroutine and StopCoroutine. Thanks for the help all! Problem solved, and I’m feeling a lot more comfortable with C# scripting.

For anyone else confused by this issue, the code I ended up finishing with is below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;

public class PlayAd : MonoBehaviour
{
public static PlayAd Instance { set; get; }
public void Start()
{
    Instance = this;
    DontDestroyOnLoad(gameObject);
    if(Advertisement.isSupported)
    { 
        Advertisement.Initialize("1370212", true); // ...initialize.
    }
    StartCoroutine("Check");

}

IEnumerator Check ()
{
    while (!Advertisement.isInitialized || !Advertisement.IsReady())
    {
        yield return new WaitForSeconds(0.5f);
    }
    if (Advertisement.IsReady())
    {
        var showOptions = new ShowOptions
        {
            resultCallback = HandleShowResult
        };
        Advertisement.Show(showOptions);
        StopCoroutine("Check");
    }   LevelLoad();
    
    
}



public string LoadLevel
{
    get
    {
        return loadLevel;
    }

    set
    {
        loadLevel = value;
    }
}

[SerializeField] private string loadLevel;
private void HandleShowResult(ShowResult result)
{
    switch (result)
    {
        case ShowResult.Finished:
            StopCoroutine("Check");
            LevelLoad();
            break;

        case ShowResult.Skipped:
            StopCoroutine("Check");
            LevelLoad();
            break;

        case ShowResult.Failed:
            StopCoroutine("Check");
            LevelLoad();
            break;
    }
}
public void LevelLoad()
{
    SceneManager.LoadScene(loadLevel);

}

}