1

I have had no issues with Unity test ads, they were displaying fine with no errors on the Android build.

But then as I was nearing release, I turned "test mode" off ads in the editor and now all my references to Advertisement in my code suddenly don't exist in the current context.

Error:

Assets\Scripts\AdManager.cs(32,21): error CS0103: The name 'Advertisement' does not exist in the current context

I haven't found anything online about this and I'm really confused. Have tried the "turn ads off, reopen unity, turn ads on" but it didn't get rid of the error.

Here's my code:

using UnityEngine;
using UnityEngine.Advertisements;

public class AdManager : MonoBehaviour
{

    public void ShowAd(int roundScore){

        Debug.Log("Recieved request to display an ad");
        Debug.Log(roundScore);

        if(roundScore >= 40){

            Debug.Log("Trying to show an ad because score is larger than 40");

            // Check for ad + show it
            if(Advertisement.IsReady("video")){
                Advertisement.Show("video");
            }
        }else{

            Debug.Log("Trying to show a random ad");

            if(Random.Range(0, 7) == 4){

                Debug.Log("Random ad will be shown if ready");

                //Check for ad + show it
                if(Advertisement.IsReady("video")){

                    Debug.Log("An ad was ready so it is being shown");
                    Advertisement.Show("video");
                }
            }
        }

    }
}

Thanks for the help.

Lumiobyte
  • 65
  • 1
  • 8
  • 2
    Did you install the [`Advertisement`/`Ads`](https://docs.unity3d.com/Packages/com.unity.ads@3.4/manual/index.html) package via the [PackageManager](https://docs.unity3d.com/Packages/com.unity.package-manager-ui@1.8/manual/index.html)? – derHugo Mar 06 '20 at 12:13
  • It was installed v2.0.8, updated to 3.4.2, and now I have heaps of errors like: ```GUID [db87dc7dfd26443b898812877180d0a4] for asset 'Packages/com.unity.ads/Editor/Resources/Purchase.png' conflicts with: 'Assets/Editor/Resources/Purchase.png' (current owner) We can't assign a new GUID because the asset is in an immutable folder. The asset will be ignored. ``` That made it worse, it appears. – Lumiobyte Mar 06 '20 at 12:23

3 Answers3

0

delete the sdk from the package manager and also if you have enabled the built-in ads extension in the ads tab under unity services just disactivate it. Then install and import the package from asset store inside the project. The problem will be resolved. Happy coding :)

Neorm
  • 1
0

Here is what i have learnt overtime about this error and errors of this type related to unity ads.

  1. Update unity editor version. Somehow it imports the files properly during upgrade and it solves the issue

2.1) Cover imports & other unity ads execution code under UNITY_ADS macro

#if UNITY_ADS
using UnityEngine.Advertisements;
#endif

2.2) Cover imports & other unity ads specific to platform since unity ads only works on these 2 as of now

#if UNITY_IOS || UNITY_ANDROID
using UnityEngine.Advertisements;
#endif

3.) Cover iOS specific imports under UNITY_IOS macro

#if UNITY_IOS
using UnityEngine.Advertisement.IosSupport;
#endif
  1. Visit 'Services' Tab then.. Unlink unity project -> Reopen -> Link project again

  2. Delayed sdk start. Instead if initiating unity on start() or awake(), do it in update() after 1-2 secs. Reason for this has something to do with busy threads which aren't very clear but it does work

xySVerma
  • 941
  • 9
  • 12
0

In a youtube video, Why Are My Unity Ads Not Working? - Advertisement does not exist, "Info Gamer" explains that if you've done everything else, you may need to force Unity to rebuild the Library directory by saving and closing your project, navigating to the folder and deleting it. Reload the project and reset your Build Settings to use Android or IOS and this error could be gone. At least, it worked for me and has worked for many others.

It might be as simple as switching back and forth between WebGL and Android or IOS, but I did it the way he suggested.

TecBrat
  • 3,643
  • 3
  • 28
  • 45