![](/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.