Search Unity

"AddComponent with MonoBehaviour is not allowed" error in UnityAds

Discussion in 'Editor & General Support' started by Bawenang-Get-Wrecked, Aug 3, 2016.

  1. Bawenang-Get-Wrecked

    Bawenang-Get-Wrecked

    Joined:
    Apr 13, 2016
    Posts:
    4
    Hi guys, I've got an error that I think is inside the Unity Ads API after updating to the stable build Unity 5.4.0f3.

    How do I fix this? Thanks.
     
  2. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    I modified AsyncExec.cs to make it work:

    Code (CSharp):
    1.  
    2. #if UNITY_ANDROID || UNITY_IOS
    3.  
    4. namespace UnityEngine.Advertisements {
    5.   using UnityEngine;
    6.   using System.Collections;
    7.  
    8.   class CoroutineHost : MonoBehaviour { }
    9.  
    10.   internal class AsyncExec {
    11.     private static GameObject asyncExecGameObject;
    12.     private static MonoBehaviour coroutineHost;
    13.     private static AsyncExec asyncImpl;
    14.     private static bool init = false;
    15.  
    16.     private static MonoBehaviour getImpl() {
    17.       if(!init) {
    18.         asyncImpl = new AsyncExec();
    19.         asyncExecGameObject = new GameObject("Unity Ads Coroutine Host") { hideFlags = HideFlags.HideAndDontSave };
    20.         coroutineHost = asyncExecGameObject.AddComponent<CoroutineHost>();
    21.         Object.DontDestroyOnLoad(asyncExecGameObject);
    22.         init = true;
    23.       }
    24.  
    25.       return coroutineHost;
    26.     }
    27.  
    28.     private static AsyncExec getAsyncImpl() {
    29.       if(!init) {
    30.         getImpl();
    31.       }
    32.  
    33.       return asyncImpl;
    34.     }
    35.  
    36.     public static void runWithCallback<K,T>(System.Func<K,System.Action<T>,IEnumerator> asyncMethod, K arg0, System.Action<T> callback) {
    37.       getImpl().StartCoroutine(asyncMethod(arg0, callback));
    38.     }
    39.   }
    40. }
    41.  
    42. #endif
     
    andyblem, electroid and sutarat9 like this.
  3. Bawenang-Get-Wrecked

    Bawenang-Get-Wrecked

    Joined:
    Apr 13, 2016
    Posts:
    4
    Thanks a lot. But this should've been fixed by the Unity guys themselves, right? I mean, this is their standart feature and it has errors.
     
  4. yumigai

    yumigai

    Joined:
    Oct 29, 2016
    Posts:
    1
    Thank you very much. I was helped.
     
  5. andyblem

    andyblem

    Joined:
    Nov 11, 2014
    Posts:
    26
    A great solution. Thank you very much.