IsObjectMonoBehaviour can only be called from the main thread.
See original GitHub issueThank you for the great project! This project saved me and many others’ lives 👍
The issue is that when async function (in this case post) is called NOT on the main thread, it gives me following error.
UnityEngine.UnityException: IsObjectMonoBehaviour can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
at (wrapper managed-to-native) UnityEngine.MonoBehaviour.IsObjectMonoBehaviour(UnityEngine.Object)
at UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) [0x00013] in C:\buildslave\unity\build\Runtime\Export\Scripting\MonoBehaviour.bindings.cs:88
at Proyecto26.StaticCoroutine.StartCoroutine (System.Collections.IEnumerator coroutine) [0x00005] in <c228329a125b4f79b199d694e8a776bd>:0
at Proyecto26.RestClient.Request (Proyecto26.RequestHelper options, System.Action`2[T1,T2] callback) [0x00007] in <c228329a125b4f79b199d694e8a776bd>:0
at Proyecto26.RestClient.Post (Proyecto26.RequestHelper options, System.Action`2[T1,T2] callback) [0x0000b] in <c228329a125b4f79b199d694e8a776bd>:0
at Proyecto26.RestClient.Post (Proyecto26.RequestHelper options) [0x00006] in <c228329a125b4f79b199d694e8a776bd>:0
Environment is: Unity 2019.1.0f2, .NET 4.x, Mono backend., Proyecto26.RestClient downloaded from master at 2020.3.12
Is this working as intended? If so, is there any practical workaround (to use on a non-main thread) ? Any help / advice will be greatly appreciated. Thank you in advance!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Related StackOverflow Question
Hello! After a long discussion with our game engine developer, it turned out as the specific code (which I added the RestClient to) was deliberately called from a separated thread, OTHER than unity main thread. Our problem has disappeared by adjusting the location of the communication functions (which is IN the unity main thread) I dont know RestClient was meant to work even if it was called from a separated thread or not. but at this point it would be efficient to close this issue and reopen when it is needed!
Thank you for all your support and again for this great work!
Sorry I cant share all of the relevant codes (we are working for some other people so it’s not entirely up to us)
part of the code which calls RestClient functions:
above code is called (through some instances and methods) from a monobehavior script attached on the gameobject which controls the entire game. sorry i cant share more about this …
Altough I have confirmed that:
When above error happens, it is actually on a non-main thread by using and comparing System.Thread.CurrentThread
The game uses UniRx package & it’s multithreading functions :https://github.com/neuecc/UniRx
I confirmed that inside of DontDestroyOnLoad, gameobject “Static Coroutine RestClient” is created and continues working when the same function is called on the main thread. (and continues giving us the same error when called on a non-main thread) (there also is DontDestroyOnLoad/MainThreadDispatcher which is created by UniRX package)
If you need any more info about this please let me know! Thank you!!