- Home /
EditorPrefs Silently fail to work when called from a server response call back.
Hi,
I have a server response in some editor code, on the response i save some strings to editor prefs. However, this fails silently. I know this works when calling it directly and i know that the code call "EditorPrefs.SetString" is being called.
Is this a bug or am I not understanding EditorPrefs.
Thanks.
Answer by Bunny83 · Jul 31, 2019 at 11:44 PM
Pretty much non of the Unity or UnityEditor API is thread-safe. Most API methods will actually throw an exception if used from a different thread than the main thread. Exceptions thrown inside your own threads are not necessarily caught by Unity. EditorPrefs.SetString does have a thread protection and will throw an exception like this:
UnityException: SetString_Internal can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. UnityEditor.EditorPrefs.SetString
If your callback is actually executed from a different thread, it's possible that the calling code in that thread will silently swallow the exception.
If your "server response" is not invoked on a seperate thread, you should be more specific. Because some "editor code" and "a server response" could mean almost anything.
Your answer
