crashes when trying to use ads

whenever i try to play the game in editor mode it works. but when i turn on the ads service it just crashes. how can i fix this?

this is my script

Code

using UnityEngine;
using UnityEngine.Advertisements;

public class ads : MonoBehaviour
{
    public test test;

    private void Start()
    {
        test = test.GetComponent<test>();
    }

    public void ShowRewardedAd()
    {
        if (Advertisement.IsReady("rewardedVideo"))
        {
            var options = new ShowOptions { resultCallback = HandleShowResult };
            Advertisement.Show("rewardedVideo", options);
        }
    }

    private void HandleShowResult(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                Debug.Log("The ad was successfully shown.");
                test.watchedAd();
                break;
            case ShowResult.Skipped:
                Debug.Log("The ad was skipped before reaching the end.");
                break;
            case ShowResult.Failed:
                Debug.LogError("The ad failed to be shown.");
                break;
        }
    }
}

it gives me this error:

Assets/ads.cs(2,19): error CS0234: The type or namespace name `Advertisements' does not exist in the namespace `UnityEngine'. Are you missing an assembly reference?

and this one:

Assets/ads.cs(22,35): error CS0246: The type or namespace name `ShowResult' could not be found. Are you missing an assembly reference?

I guess you are building for desktop, i.e. not Android or iOS? Our recommended approach is to encapsulate your ads code with “#if UNITY_ADS” as shown in examples on e.g. https://docs.unity3d.com/ScriptReference/Advertisements.Advertisement.Show.html

Does that solve your problem?

/Rasmus

1 Like

i will try it but i am making it for android
https://play.google.com/store/apps/details?id=com.RadioActiveStudios.TrumpClicker&hl=nl

it worked!!! in the unity service tab it gives you a different sample code. without that sample code and the code from your link it worked.

thanks alot