# crashes when trying to use ads

**URL:** https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983
**Category:** Unity Ads
**Tags:** Unity-Ads-and-User-Acquisition
**Created:** [April 8, 2017, 2:13pm UTC](https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983 "2017-04-08T14:13:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kelvin-w](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelvin-w/32/497826_2.png) [@kelvin-w](https://discussions.unity.com/u/kelvin-w)
#### Post date: [April 8, 2017, 2:13pm UTC](https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983/1 "2017-04-08T14:13:41Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kelvin-w](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelvin-w/32/497826_2.png) [@kelvin-w](https://discussions.unity.com/u/kelvin-w)
#### Post date: [April 8, 2017, 2:37pm UTC](https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983/2 "2017-04-08T14:37:11Z")

</div>

this is my script

Code

```csharp
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:

```csharp
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:

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

```

---

<div class="post-metadata">

### Author: ![rasmus-unity](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rasmus-unity](https://discussions.unity.com/u/rasmus-unity)
#### Post date: [April 9, 2017, 3:02am UTC](https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983/3 "2017-04-09T03:02:11Z")

</div>

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](https://docs.unity3d.com/ScriptReference/Advertisements.Advertisement.Show.html)

Does that solve your problem?

/Rasmus

---

<div class="post-metadata">

### Author: ![kelvin-w](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelvin-w/32/497826_2.png) [@kelvin-w](https://discussions.unity.com/u/kelvin-w)
#### Post date: [April 9, 2017, 9:19am UTC](https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983/4 "2017-04-09T09:19:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kelvin-w](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelvin-w/32/497826_2.png) [@kelvin-w](https://discussions.unity.com/u/kelvin-w)
#### Post date: [April 9, 2017, 9:53am UTC](https://discussions.unity.com/t/crashes-when-trying-to-use-ads/660983/5 "2017-04-09T09:53:11Z")

</div>

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
