![](/img/trans.png)
[英]Error in unity(Script Inspector 3): GetBool is not allowed to be called from a ScriptableObject constructor
[英]ScriptableObject.ctor and LoadAssetAtPath error in unity 5.4
每次播放場景時都會出現這三個錯誤。 有沒有問題的錯誤,但我不明白為什么我得到了這個。
1
不允許從ScriptableObject構造函數(或實例字段初始化程序)中調用ScriptableObject.ctor,而應在OnEnable中調用它。 從ScriptableObject'Find_Object_by_component'調用。 有關更多詳細信息,請參見《 Unity手冊》中的“腳本序列化”頁面。 UnityEditor.SceneView:.ctor()Find_Object_by_component:.ctor()
2
不允許從ScriptableObject構造函數(或實例字段初始化程序)中調用LoadAssetAtPath,而應在OnEnable中調用它。 從ScriptableObject'Find_Object_by_component'調用。 有關更多詳細信息,請參見《 Unity手冊》中的“腳本序列化”頁面。 UnityEditor.SceneView:.ctor()Find_Object_by_component:.ctor()
3
UnityException:不允許從ScriptableObject構造函數(或實例字段初始值設定項)調用LoadAssetAtPath,而應在OnEnable中調用它。 從ScriptableObject'Find_Object_by_component'調用。 有關更多詳細信息,請參見《 Unity手冊》中的“腳本序列化”頁面。 UnityEditor.EditorGUIUtility.Load(System.String文件名,System.Type類型)(位於C:/buildslave/unity/build/artifacts/Generated/common/editor/EditorGUIUtilityBindings.gen.cs:470)UnityEditor.EditorGUIUtility.Load(系統.String路徑)(在C:/buildslave/unity/build/artifacts/Generated/common/editor/EditorGUIUtilityBindings.gen.cs:464)UnityEditor.EditorGUIUtility.LoadGeneratedIconOrNormalIcon(System.String名稱)(在C:/ buildslave / unity /build/artifacts/generation/common/editor/EditorGUIUtilityBindings.gen.cs:182)UnityEditor.EditorGUIUtility.LoadIconForSkin(System.String名稱,Int32 skinIndex)(在C:/ buildslave / unity / build / artifacts / Generated / common / editor / EditorGUIUtilityBindings.gen.cs:206)UnityEditor.EditorGUIUtility.LoadIcon(System.String名稱)(位於C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:177)UnityEditor。 EditorGUIUtility.LoadIconRequired(System.String name)(在C:/ buildslave / unity / build / artifacts / generated / common / editor / EditorGUIU tilityBindings.gen.cs:167)UnityEditor.EditorGUIUtility.TextContentWithIcon(System.String textAndTooltip,System.String圖標)(位於C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:117 )UnityEditor.EditorWindow.GetLocalizedTitleContentFromType(System.Type t)(在C:/buildslave/unity/build/Editor/Mono/EditorWindow.cs:119)UnityEditor.EditorWindow.GetLocalizedTitleContent()(在C:/ buildslave / unity / build /Editor/Mono/EditorWindow.cs:102)UnityEditor.SceneView.OnEnable()(在C:/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:350)UnityEditor.SceneView:.ctor()Find_Object_by_component :.ctor()
除非您確切知道自己在做什么以及為什么需要打破規則,否則ScriptableObject
構造函數不應用於任何事情。 原因是ScriptableObjects
不是在Unity主線程中構造的,因此您不能真正使用Unity API。 ( Debug.Log
是可以在任何線程中使用的少數Unity函數之一。)
使用Awake
或OnEnable
函數進行初始化。 如果是MonoBehaviour
,則還可以使用Start
。 這些由游戲引擎自動調用。
如果您確實找不到令人討厭的類,則可以使用find / replace將所有函數調用從AssetDatabase.LoadAssetAtPath
為MyDebugClass.LoadAssetAtPath
。 接下來,定義:
#line hidden
public static class MyDebugClass
{
public static Object LoadAssetAtPath(string assetPath, Type type)
{
Debug.Log("LoadAssetAtPath called: " + assetPath);
return AssetDatabase.LoadAssetAtPath(assetPath, type);
}
}
#line default
此hack將向您顯示每個呼叫者,錯誤之前的消息將直接指向罪魁禍首。 (這不是一種優雅的技術,但是它將為您提供快速的答案。盡管目前我無法檢查語法。)
注意: #line hidden
表示此功能不會顯示在堆棧跟蹤中。 因此,堆棧跟蹤的第一行將是您感興趣的行,而不是MyDebugClass
內的MyDebugClass
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.