- Home /
The question is answered, right answer was accepted
Values set by [ExecuteInEditMode] script revert back in play mode.
Hi. I've got a problem. I've got some [ExecuteInEditMode] script that calculates some values that need to be public (there are 2 modes - it calculates value 1 based on value 2 input or the opposite, so I need to be able to edit both in the inspector). The thing is, when I press play, the value that was set by script reverts back to whatever I put there by hand the last time (or to 0 if I never did). I saw a "solution"that requires saving the values in editor prefs. Sounds really like too much oh a hustle to me - there are gonna be 100+ levels with multiple objects per scene using this specific script. I'd prefer reentering the calculated value by hand every time it's done by script than saving all them as editor prefs, but this is also quite annoying. IS there any better workaround to make Unity treat values set by script as if I put them by hand?
Edit: Or did I misunderstand the editor prefs solution? Can I just save value in some holder field in prefs and load it right away every time it's calculated?
Edit2: I didn't. Just checked. Saving and loading doesn't make the value stay in play mode. I can't load the value from editor prefs in play mode, cause I won't be able to do it outside Unity anyway, so this is not a viable workaround.
Answer by jamqdlaty · Jun 13, 2020 at 09:38 AM
Ok, found a solution myself. In the script, before the value changes, I call: Undo.RecordObject(this,"name of action I want visible in Undo"); This record any changes made to the object in current update and somehow saves them.
Follow this Question
Related Questions
Setting variables in public class to values from editor script 3 Answers
Per-application versus per-level editor values 2 Answers
Saving dictionary from editor mode in play mode 1 Answer
Editor Script - Multi-Editing Value Increase/Decrease Issue 1 Answer
What's the C# equivalent of @script ExecuteInEditMode() ? 2 Answers