- Home /
How can I save scripted variable changes?
Right now, I have some monobehaviours attached to gameObjects within my scene. There are public variables in these monobehaviours that I can change in the inspector.
If I change the values manually, they become bold in the inspector and save when I save the scene.
If I change the values via another script, they don't become bold in the inspector and don't save when I save the scene but instead reset to the default values of the script.
I would like to have Unity save values that have been changed via a script. Is there a way to enable this?
EDIT: I should clarify that the scripts I'm using are Editor scripts that I run from the Window dropdown. The variable changes are made without entering playmode.
Answer by Jeff-Kesselman · Jun 03, 2014 at 05:40 PM
Short answer is no. All changes that occur during gameplay are reset at the end of the gameplay.
This is behavior you absolutely need for development purposes.
Longer answer, you can write scripts that are executed during editing rather then just during gameplay.
To do that you use the http://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html attribute.
You can also write custom editor code to build new functions into the inspector and editor.
I think the ExecuteInEdit$$anonymous$$ode may be exactly what I needed. I'll give it a try and post back. Thanks!
EDIT: I added [ExecuteInEdit$$anonymous$$ode] to the top of my editor scripts above the class declaration but the values still don't stick when I save the scene, close it, and reopen it. Am I doing it wrong?
You may have to tell it its been changed
See http://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
That did it! After I changed the value, I used SetDirty on it and it saves with the scene now. I took out ExecuteInEdit$$anonymous$$ode because it worked without it(I think because I was using Editor scripts).
Thanks! That's a huge help.
Answer by loz999 · Jun 03, 2014 at 06:10 PM
Be careful when using executeInEditMode as you can change all sorts of stuff that you don't want to that won't revert when you exit. Are you wanting something similar to ApplicationSettings in visual studio to save progress, level, XP, gear etc. or is there another reason?
I was using it to quickly edit items that would later be spawned in game. I ended up removing the ExecuteInEdit$$anonymous$$ode and just used SetDirty ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
Assign Color[] var in prefabs using Editor Scripting 1 Answer
Custom Transform Editor in Scene Editor 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to change variables from a script in another scene? 1 Answer
EventSystem micro lag on the first touch 0 Answers