简体   繁体   中英

ScriptableObject.ctor and LoadAssetAtPath error in unity 5.4

I am getting these three errors each time i playing my scene. There error making no problem but i am unable to understand that why i am getting this.

1

ScriptableObject.ctor is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'Find_Object_by_component'. See "Script Serialization" page in the Unity Manual for further details. UnityEditor.SceneView:.ctor() Find_Object_by_component:.ctor()

2

LoadAssetAtPath is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'Find_Object_by_component'. See "Script Serialization" page in the Unity Manual for further details. UnityEditor.SceneView:.ctor() Find_Object_by_component:.ctor()

3

UnityException: LoadAssetAtPath is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject 'Find_Object_by_component'. See "Script Serialization" page in the Unity Manual for further details. UnityEditor.EditorGUIUtility.Load (System.String filename, System.Type type) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:470) UnityEditor.EditorGUIUtility.Load (System.String path) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:464) UnityEditor.EditorGUIUtility.LoadGeneratedIconOrNormalIcon (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:182) UnityEditor.EditorGUIUtility.LoadIconForSkin (System.String name, Int32 skinIndex) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:206) UnityEditor.EditorGUIUtility.LoadIcon (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:177) UnityEditor.EditorGUIUtility.LoadIconRequired (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIU tilityBindings.gen.cs:167) UnityEditor.EditorGUIUtility.TextContentWithIcon (System.String textAndTooltip, System.String icon) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:117) UnityEditor.EditorWindow.GetLocalizedTitleContentFromType (System.Type t) (at C:/buildslave/unity/build/Editor/Mono/EditorWindow.cs:119) UnityEditor.EditorWindow.GetLocalizedTitleContent () (at C:/buildslave/unity/build/Editor/Mono/EditorWindow.cs:102) UnityEditor.SceneView.OnEnable () (at C:/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:350) UnityEditor.SceneView:.ctor() Find_Object_by_component:.ctor()

A ScriptableObject constructor should not be used for anything, unless you know exactly what you're doing and why you need to break the rules. The reason is that ScriptableObjects aren't constructed in the main Unity thread, thus you can't really use the Unity API. ( Debug.Log is one of the few Unity functions you can use in any thread.)

Use the Awake or OnEnable functions for initialization. If it's a MonoBehaviour , you can also use Start . These are called automatically by the game engine.

If you really can't find the offending class, you can use find/replace to change all the function calls from AssetDatabase.LoadAssetAtPath to MyDebugClass.LoadAssetAtPath . Next, define:

#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

This hack will show you each caller, and the message directly before the error will point to the culprit. (Not an elegant technique, but it will give you a fast answer. Though I can't check the syntax at the moment.)

Note: #line hidden means this function won't show up in the stack trace. So the top line of your stack trace will be the line you're interested in, rather than a line inside MyDebugClass .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM