- Home /
Assign value to SerializedProperty in EditorWindow
I need to assign a property, in EditorWindow, obtained using SerializedProperty.
SerializedObject(myScript).FindProperty("myClass.myFloat").floatValue = 9.8;
Debug.Log(SerializedObject(myScript).FindProperty("myClass.myFloat").floatValue);
It works when I assign a value in PropertyDrawer.
It does not work when I assign a value in EditorWindow.
So is there a way for me to apply the change I have made to the SerializedProperty, or stop it from being reset ?
alternatively : I would like to get my custom class from a SerializedProperty and change it there. How would I do so ?
eg.
var whatIHave : SerializedProperty; var whatINeed : MyCustomClass;
whatINeed = whatIHave as MyCustomClass;
whatINeed.myFloat = 9.8;
Answer by Dazdingon · May 08, 2014 at 09:55 AM
Solved it!
All I had to do was call ApplyModifiedProperties() on the SerializedObject I was changing.
PropertyDrawer does this automatically, EditorWindow does not.
Note : Same applies for EditorGUI.PropertyField() outside of a PropertyDrawer.
I also had to call Update() after Apply$$anonymous$$odifiedProperties() for it to work for me.
Note really. You should call Update before you read and modify your properties and finish with Apply$$anonymous$$odifiedProperties. That's the normal procedure. It's explained in the docs, though as usual not that clearly ^^. The examples might also be misleading because creating a SerializedObject will automatically call Update. Also the default behaviour of OnInspectorGUI will also call Update automatically.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Triggering updates in a custom EditorWindow 0 Answers
Editor: Drawing outside the own window 0 Answers
Multiple editor windows combined 0 Answers
Reflection on class variable values null on editor startup 0 Answers
Editor Color Picker issues 0 Answers