HttpClient.cs : UnityException: Internal_CreateGameObject is not allowed to be called from a MonoBehaviour constructor
Closed this issue · 1 comments
Hi,
I was wondering why is this still a thing, or maybe there's something that i'm missing about this behavior in the code,
however it does seem like you need to instantiate an object with the dispatcher in line 448:
private void CreateDispatcherGameObject()
{
if (_dispatcher == null)
{
_dispatcher = new GameObject(DISPATCHER_GAMEOBJECT_NAME).AddComponent();
}
}
I've seen this issue also in another google ads plugin, and i'm wondering when will you fix this.
Currently i'm not sure that I need the dispatcher, however I only just bought the package.
at any case I would suggest to fix this like this (this is how I validate my singletons anyway) :
private static IDispatcher i_dispatcher = null;
public static IDispatcher _dispatcher
{
get
{
if (i_dispatcher == null)
{
i_dispatcher = new GameObject().AddComponent();
}
return i_dispatcher;
}
}
Thanks,
Kobi
Hi @kobi666, how are you constructing the HttpClient instance? Sounds like you are doing it from a MonoBehaviour constructor, can you do it elsewhere?