close
我的環境配置:Windows 10、Unity 5.6.0f3。
如果遇到了以下錯誤:
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

大致上是因為你的 GameObject 只是載入,而尚未 實體化(Instance),所以這時候執行了 SetParent() 方法時,就會出現該錯誤了。
因為系統認為你不應該直接去修改原始資源檔。
解決辦法如下:
// path 為資源路徑, parent 為父層 transform. Object source = Resources.Load( path ); GameObject gobj = Instantiate( source ); gobj.transform.SetParent( parent );
以下作法會造成錯誤:
GameObject gobj = Resources.Load< GameObject >( path ); gobj.transform.SetParent( parent );
因為沒有使用 Instantiate() 進行實體化,所以會出現 Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. 的錯誤訊息。
文章標籤
全站熱搜