IsObjectMonoBehaviour can only be called from the main thread.

See original GitHub issue

Thank 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Marcus9K720commented, Apr 20, 2020

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!

2reactions
Marcus9K720commented, Mar 17, 2020

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:

   public void SendAllLogs(string bigJson)
    {
        string sendAllLogsUrl = restBaseUrl + "/logs";> 

        byte[] bigJsonUTF8CompB = CompressionHelper.CompressBytes(
                System.Text.Encoding.Convert(
                    System.Text.Encoding.Default, System.Text.Encoding.UTF8,
                    System.Text.Encoding.Default.GetBytes(bigJson)
                    )

            );

        RequestHelper rh = new RequestHelper
        {
            Uri = sendAllLogsUrl,
            Method = "POST",
            Headers = this.headers,
            ContentType = "application/octet-stream",
            BodyRaw = bigJsonUTF8CompB,
        };
        
        RestClient.Post(rh).Then(res =>
        {
            JsonData data = JsonMapper.ToObject(res.Text);

            // some codes to run when successful                 

        }
        ).Catch(err =>
        {
            // Log Error
        }
        );
    }

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:

  1. When above error happens, it is actually on a non-main thread by using and comparing System.Thread.CurrentThread

  2. The game uses UniRx package & it’s multithreading functions :https://github.com/neuecc/UniRx

  3. 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!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: IsObjectMonoBehaviour can only be called from the ...
Error : IsObjectMonoBehaviour can only be called from the main thread. I am using Gley Mobile Ads to show rewarded video and then...
Read more >
get_gameObject can only be called from the main thread. ...
This is the error: UnityException: get_gameObject can only be called from the main thread. Constructors and field initializers will be executed ...
Read more >
How to fix "get_transform can only be called in main thread ...
Hi I've made a simple code where a 3D object will appear in the scene when it receives an MQTT message from an...
Read more >
TrySetSetString can only be called from the main thread.
I am using the Auth Sample in there to Signing an email/pass user: I receive an error: TrySetSetString can only be called from...
Read more >
get_gameObject can only be called from the main thread
Any idea why come out this error? get_gameObject can only be called from the main thread. Constructors and field initializers will be executed ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found