- Home /
Custom WindowEditor, but variable sets back to 0 when hit Play
I have lots of single GameObjects with a specific script. This script has an variable called id. I've also made a custom WindowEditor that iterates through all selected GameObjects and gives them a distinct id. It works fine, but every time I hit the Play button, all ids are set back to 0.
Answer by ShroomWasTaken · Jan 10, 2018 at 05:35 PM
I'm not sure why, but I believe you actually need to use a SerializedObject and SerializedProperty in order to make communication between variables from editor to a normal script.
You probably need to do something along the lines of this in your EditorWindow script:
private SerializedObject idSerializedObject;
private SerializedProperty idSerializedProperty;
private void OnEnable()
{
idSerializedObject = new SerializedObject(targetScript);
idSerializedProperty = idSerializedObject.FindPropert("id");
// In order to change the target variable you have a bunch of "value" properties in the SerializedProperty class you can use.
idSerializedProperty.intValue = 999;
// And then you have to apply the modified value to the object.
idSerializedObject.ApplyModifiedProperties();
}
Your answer
Follow this Question
Related Questions
Move objects in scene view before running the game 0 Answers
Variable not being saved? 0 Answers
Variables in editor script reset when playing 1 Answer