Search code examples
c#unity-game-engineunityads

why such an error occurs unity monetization


I added unity monetization package then I write this script:

using UnityEngine;
using UnityEngine.Advertisements;

public class ADSSSS : MonoBehaviour
{
    private void Start()
    {
        Advertisement.Initialize("my google play ID", false);
    }
}

then in unity such an error occurred

Can't add script behaviour CoroutineExecutor because it is an editor script. To attach a script it needs to be outside the 'Editor' folder. UnityEngine.Advertisements.Advertisement:.cctor () ADSSSS:Start () (at Assets/Scripts/ADSSSS.cs:8)

Can't add script behaviour ApplicationQuit because it is an editor script. To attach a script it >needs to be outside the 'Editor' folder. UnityEngine.Advertisements.Advertisement:.cctor () ADSSSS:Start () (at Assets/Scripts/ADSSSS.cs:8)

Initializing Unity Ads. UnityEngine.Advertisements.Advertisement:.cctor () ADSSSS:Start () (at Assets/Scripts/ADSSSS.cs:8)

Object reference not set to an instance of an object UnityEngine.Advertisements.Advertisement:.cctor () ADSSSS:Start () (at Assets/Scripts/ADSSSS.cs:8)

Can't add script behaviour CoroutineExecutor because it is an editor script. To attach a script it needs to be outside the 'Editor' folder. UnityEngine.Advertisements.Advertisement:.cctor () ADSSSS:Start () (at Assets/Scripts/ADSSSS.cs:8)

Can't add script behaviour ApplicationQuit because it is an editor script. To attach a script it needs to be outside the 'Editor' folder. UnityEngine.Advertisements.Advertisement:.cctor () ADSSSS:Start () (at Assets/Scripts/ADSSSS.cs:8)

NullReferenceException: Object reference not set to an instance of an object UnityEngine.Advertisements.Utilities.UnityLifecycleManager.Initialize () (at <91220002a2fd403abd0d44e0c6cd5f2c>:0) UnityEngine.Advertisements.Utilities.UnityLifecycleManager..ctor () (at <91220002a2fd403abd0d44e0c6cd5f2c>:0) UnityEngine.Advertisements.Advertisement.CreatePlatform () (at <91220002a2fd403abd0d44e0c6cd5f2c>:0) UnityEngine.Advertisements.Advertisement..cctor () (at <91220002a2fd403abd0d44e0c6cd5f2c>:0) Rethrow as TypeInitializationException: The type initializer for 'UnityEngine.Advertisements.Advertisement' threw an exception. ADSSSS.Start () (at Assets/Scripts/ADSSSS.cs:8)


Solution

  • From Advertisement.Initialize

    Manually initializes the advertisement system. Normally this is done from editor, and you should only call this method if you are using UnityAds with automatic initialization disabled.

    the error you get is because certain scripts are placed in a folder called Editor. The content of these folders as the name suggests is supposed to be only used in the Unity Editor and will be skipped when building your application.

    So in a build they won't be available anyway. Therefore Unity also forbids to use components from these folders in PlayMode.

    Unity doesn’t allow components derived from MonoBehaviour to be assigned to GameObjects if the scripts are in the Editor folder.


    To help further it would be required that you tell us the exact Unity and Advertisement versions you are using and how exactly you installed the Advertisement into your project. It is possible that the manual you are referring to is from a different version and things changed between them.