Unity3d reports a strange error CompareBaseObjectsInternal can only be called from the main thread.

Source: Internet
Author: User

The. NET Async Socket code is used in the project, and the following strange error occurs:

CompareBaseObjectsInternal 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.

The strange reason is that the error does not affect program execution. Instead, it correctly receives the information returned by the server and displays the information correctly through the callback, indicating that the callback is executed. However, the second parser called from the callback is not executed. Is it because of the above error?

Baidu CompareBaseObjectsInternal search has only eight results. This is not the same as my situation. It is of little reference significance.

Then google, unfortunately, the old brother has been loading... So when I went to yahoo, there was no cramps. The previous articles found are on the unity Forum.

Http://forum.unity3d.com/threads/comparebaseobjectsinternal-error.184069/#post-1258700

 

Http://forum.unity3d.com/threads/comparebaseobjectsinternal-can-only-be-called-from-the-main-thread-annoying.195902/http://forum.unity3d.com/threads/comparebaseobjectsinternal-can-only-be-called-from-the-main-thread-annoying.195902/

 

Http://community.kii.com/t/error-comparebaseobjectsinternal-can-only-be-called-from-the-main-thread/378!

 

Http://answers.unity3d.com/questions/704284/comparebaseobjectsinternal-can-only-be-called-from-1.html

My problem was probably because I registered a callback in the main thread,

// Register the response code for all events
NetEventManager. GetInstance (). netMessageReceived_event + = OnNetMessageReceived;
NetEventManager. GetInstance (). gateServerConnected_event + = OnGateServerConnected;
NetEventManager. GetInstance (). errorMessageOccured_event + = OnErrorReceived;

Then the socketclient object is asynchronous, so multithreading is implemented. When the socketclient receives the message, it calls back the main thread function (inherited from monobehaviour), which leads to an error. The first blog post in the previous unity Forum clearly explains that unity imposes restrictions on the main API calling thread:

"Unity chose to limit API cballs to main-thread, to make a simple and solid threading model that everyone can understand use ."

According to this explanation, I comment out the above Code and the program will not report an error. It is estimated that the event response code should be placed in a non-monobehavior class to avoid the thread security issues mentioned above.

 

Since I am still a little bit familiar with multi-threaded programming, whether the above explanation is correct or not, is there any better solution? I hope you can give me some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.