Application.persistentDataPath失败

Application.persistentDataPath Failed

我已经将我的 unity 更新到 2017.4.0f1 并且我有关于保存一些信息的脚本 在类似文本的文件中。我用过

public static string savePath = Application.persistentDataPath + "/";

但是我收到这个错误

get_persistentDataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'FileHandler' on game object 'DisplayHigh'.
See "Script Serialization" page in the Unity Manual for further details.
0x00000001412F26E6 (Unity) StackWalker::GetCurrentCallstack
0x00000001412F328F (Unity) StackWalker::ShowCallstack
0x00000001411B1BE0 (Unity) GetStacktrace
0x00000001406DB5F3 (Unity) DebugStringToFile
0x00000001406DBDD1 (Unity) DebugStringToFile
0x0000000140A267CF (Unity) ReportError
0x0000000140A27EEA (Unity) ThreadAndSerializationSafeCheckReportError
0x00000001414173D0 (Unity) Application_Get_Custom_PropPersistentDataPath
0x000000000DDC32EA (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Application:get_persistentDataPath ()
0x000000000DDC3145 (Mono JIT Code) [FileHandler.cs:9] FileHandler:.cctor () 
0x000000000DD511DE (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
0x00007FFFBEAF64CF (mono) [mini.c:4937] mono_jit_runtime_invoke 
0x00007FFFBEA48A49 (mono) [object.c:2623] mono_runtime_invoke 
0x00007FFFBEA4C39E (mono) [object.c:359] mono_runtime_class_init_full 
0x00007FFFBEAF5C09 (mono) [mini.c:4500] mono_jit_compile_method_inner 
0x00007FFFBEAF5E45 (mono) [mini.c:4556] mono_jit_compile_method_with_opt 
0x00007FFFBEAF60BB (mono) [mini.c:4775] mono_jit_runtime_invoke 
0x00007FFFBEA48A49 (mono) [object.c:2623] mono_runtime_invoke 
0x00000001407ECD19 (Unity) mono_runtime_invoke_profiled
0x0000000140A0EF95 (Unity) mono_runtime_object_init_exception
0x0000000140A2FBBB (Unity) scripting_unity_engine_object_new_and_invoke_default_constructor
0x00000001409EC099 (Unity) MonoBehaviour::RebuildMonoInstance
0x0000000140A08445 (Unity) RebuildAllScriptCaches
0x0000000140A0BD86 (Unity) MonoManager::EndReloadAssembly
0x0000000140A0C695 (Unity) MonoManager::ReloadAssembly
0x0000000140C9CC57 (Unity) ReloadAllUsedAssemblies
0x0000000140C8C628 (Unity) EditorSceneManager::RestoreSceneBackups
0x00000001411E8FBF (Unity) PlayerLoopController::EnterPlayMode
0x00000001411E98A6 (Unity) PlayerLoopController::SetIsPlaying
0x00000001411EA6F6 (Unity) Application::TickTimer
0x00000001414101DF (Unity) MainMessageLoop
0x0000000141411A9C (Unity) WinMain
0x0000000141E662B8 (Unity) __tmainCRTStartup

还有这个

UnityException: get_persistentDataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'FileHandler' on game object 'DisplayHigh'.
See "Script Serialization" page in the Unity Manual for further details.
FileHandler..cctor () (at Assets/Scripts/FileHandler.cs:9)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for FileHandler
StatCheck.Update () (at

我真的很迷茫很郁闷你知道吗

如有任何帮助,我们将不胜感激。

public static string savePath;
void Awake()
{

   MyClass.savePath = Application.persistentDataPath + "/";
}

我的 class 是包含此变量的 MonoBehavior,因为它是一个静态变量,我们需要在前面加上 class 名称。