1. 程式人生 > >Unity3d(C#)錯誤提示:禁止線上程中進行遊戲物件比較!

Unity3d(C#)錯誤提示:禁止線上程中進行遊戲物件比較!

今天剛剛寫完非同步Socket客戶端的資料收發,在進行測試的時候,Unity報出錯誤:

CompareBaseObjectsInternal can only be called from the main thread.


詳情如下:

ERROR : 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.

在Unity3d官方論壇有網友請教了這個問題

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


答案如下:

Seems like you must be calling Unity specific functions from another thread, compare gameobjects means you are checking if one gameobject equals another, apparently this is not threadsafe, so you would need to make sure your separate thread does NOT interract with Unity functions at all. 

翻譯過來就是說:

在Unity中需要遵循C#的執行緒安全,不能在另外的執行緒對Unity的GameObject進行相等比較操作。

在我的工程中,當Socket BeginConnect的時候就會 呼叫WindowManager來顯示一個等待介面

當Connect Success或者failed的時候,就會關閉。

顯示介面是在Unity執行緒中執行

關閉介面是在Socket的非同步回撥中執行

從Mono的斷點來看這兩個不是同一個執行緒。

因為在Wait.GetSingleton().Close() 中有用到WindowManager.GetSingleton()

如下:

	public static CCWindowManager GetSingleton()
	{
		if(m_sInstance==null)
		{
			//Helper.LogError("CCWindowManager m_sInstance==nul");
			GameObject obj = new GameObject("CCWindowManager");
			m_sInstance=obj.AddComponent<CCWindowManager>();
		}
		return m_sInstance;
	}

所以在判斷 m_sInstance == null 的時候出現了錯誤。

解決方法:

將關閉等待介面的程式碼移到了Update中。

相關推薦

Unity3d(C#)錯誤提示禁止線上進行遊戲物件比較

今天剛剛寫完非同步Socket客戶端的資料收發,在進行測試的時候,Unity報出錯誤: CompareBaseObjectsInternal can only be called from the main thread. 詳情如下: ERROR : CompareB

Unity3d C 錯誤提示 禁止線上進行遊戲物件比較

                今天剛剛寫完非同步Socket客戶端的資料收發,在進行測試的時候,Unity報出錯誤: &nb

C++ 錯誤提示無法將引數1從const char [8] 轉換為char *

#include <iostream> using namespace std; void test(char * p) { cout << p << endl; } int main(void) { test("geerniya")

python操作剪貼板錯誤提示pywintypes.error: (1418, 'GetClipboardData',線沒有打開的剪貼板)

obj 16px nco == 復制 enc .get color pyw 問題現象:通過打斷點,一步步調試可以正常復制和粘貼剪貼板數據。但是直接運行會報錯pywintypes.error: (1418, ‘GetClipboardData‘,線程沒有打開的剪貼板)問題原因

github常見操作和常見錯誤錯誤提示fatal: remote origin already exists.

空格 多余 因此 ear rem detail tco acs core 如果輸入$ git remote add origin [email protected]:djqiang(github帳號名)/gitdemo(項目名).git 提示出錯信息:fatal: re

錯誤提示未處理的“System.NullReferenceException”類型的異常出現在 system.data.dll 。 其他信息: 未將對象引用設置到對象的實例。

錯誤 reference tar RM 問題 一個 ext ID dll 這種情況的發生一般有3種情況:1.代碼中有一個對象沒有初始化。例如Form A=new Form();2.連接數據庫的問題。連接和打開連接的語句。3.T-SQL語句不正確,引起在訪問數據庫時有問題。錯

C語言const禁止修改變數的值

C語言const:禁止修改變數的值 有時候我們希望定義這樣一種變數,它的值不能被改變,在整個作用域中都保持固定。例如,用一個變數來表示班級的最大人數,或者表示緩衝區大小。為了滿足這一要求,可以使用const關鍵字對變數加以限定: const int MaxNum = 100; 

Mysql錯誤提示Error: Duplicate entry '0' for key 'PRIMARY'

Error: Duplicate entry '0' for key 'PRIMARY',在stackoverflow上看到的答案: ALTER TABLE your_table MODIFY some_column INT NOT NULL AUTO_INCREMENT; 原因是沒有

錯誤提示Suggestion: use tools:overrideLibrary="" to force usage

異常提示: 應用在Android Studio Build的時候,丟擲瞭如下異常: Error:Execution failed for task ‘:app:processDebugManifest’. > Manifest merger failed : uses-sd

python錯誤提示TypeError: 'builtin_function_or_method' object is not subscriptable

[] 換成 () >>> yy.replace['a','s'] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'bui

maven專案的pom.xml檔案出現紅色錯誤提示ArtifactTransferException: Failure to transfer

maven專案的pom.xml檔案出現紅色錯誤提示,滑鼠放上去提示: ArtifactTransferException: Failure to transfer org.deeplearning4j:deeplearning4j-ui-model:jar:0.7.1 f

github常見操作和常見錯誤錯誤提示fatal: remote origin already exist

如果輸入$ git remote add origin [email protected]:djqiang(github帳號名)/gitdemo(專案名).git      提示出錯資訊:fatal: remote origin already exists.     解決辦法如下:     1、先

錯誤提示'str' object has no attribute 'tzinfo'

記得關注我 這是什麼問題呢? 在資料庫中你定義了一個時間,但是這個時間欄位是字串型別的, 在資料庫查詢的時候當然會報錯,因為字串的屬性中沒有時間的屬性。 錯誤提示 ‘str’ object has no attribute ‘tzinfo’ 解決方案: 在mod

C#如何線上修改我的窗體控制元件的text文字

以下辦法實驗成功: 執行緒不能操作窗體控制元件,會引發異常,建議使用委託進行窗體控制元件的操作,包括 改text. 1、定義委託 delegate void SetTextCallback(string text); 2、實現委託 priv

算法系列-佇列佇列線上池等有限資源池的應用

整理自極客時間-資料結構與演算法之美。原文內容更完整具體,且有音訊。購買地址: 1.如何理解佇列 佇列可以把它想象成排隊買票,先來的先買,後來的人只能站末尾,不允許插隊。先進者先出,這就是典型的“佇列”。 佇列跟棧非常相似,支援的操作也很有限,最基本的操作也是兩個:入隊 enq

錯誤提示該虛擬機器似乎正在使用

VMWARE 開啟虛擬機器(ubuntu 64位 -14.04)彈出錯誤提示對話方塊: 點選獲取所有權後: 取消。 此時找到虛擬機器的安裝位置,找到.lck字尾的資料夾全部直接刪除即可。 再次開啟虛擬機器即可正常啟動,如下:

啟動eclipse出現錯誤提示an error hava occured,see the log

在Win8系統上下載了eclipse3.2,eclipse3.22壓縮包,解壓之後在eclipse的安裝目錄中雙擊eclipse.exe執行eclipse時提示an error hava occured,see the log。如下: 點選eclipse安裝目錄下的co

oracle 刪除表空間錯誤 提示ora-02429:無法刪除用於強制唯一/主鍵的索引

sql>drop tablespace ksdm ora-01549:表空間非空,請使用INCLUDING CONTENTS 選項 sql>drop tablespace ksdm INCLUDING CONTENTS and datafiles; ora-00604:遞迴sql層1出現錯誤。

xp連線VPN錯誤提示 vpn連接出錯789L2TP連線嘗試失敗,因為安全層在初始化與遠端計算機的協商時遇到一個處理錯誤

按照這個方法測試一下~ 1. 單擊“開始”,單擊“執行”,鍵入“regedit”,然後單擊“確定” 2. 找到下面的登錄檔子項,然後單擊它: HKEY_LOCAL_MACHINE\ System

C++ explicit關鍵字禁止隱式轉換

explicit關鍵字是用來防止由建構函式定義的隱式轉換。 要明白這個關鍵字的含義,首先我們要知道class型別的物件的隱式轉換:可以用單個實參來呼叫的建構函式定義了從形參型別到該類型別的一個隱式轉換。 比如: class SomeBody{ public: So