- Home /
Custom editor toggles doesn't save
Hi!
I have made a custom editor script for one of my objects and I have these bools, allowRotation, isLight for example. When I set them to true, they set to false when I start the game. If I make a prefab out of them, they somehow save. If I enter playmode while the object is selected, it doesn't save. What can I do to save it?
My code:
public override void OnInspectorGUI()
{
serializedObject.Update();
var theScript = target as LevelEditorObject;
showRotations = EditorGUILayout.Foldout(showRotations, "Object Rotation");
if(showRotations)
{
theScript.allowRotation = EditorGUILayout.Toggle("Allow Rotation", theScript.allowRotation);
if(theScript.allowRotation == true)
{
theScript.allowXRot = EditorGUILayout.Toggle("Allow X Rotation", theScript.allowXRot);
theScript.allowYRot = EditorGUILayout.Toggle("Allow Y Rotation", theScript.allowYRot);
theScript.allowZRot = EditorGUILayout.Toggle("Allow Z Rotation", theScript.allowZRot);
}
}
}
Thanks
EDIT: Just discovered that when it does save and then when I exit playmode, it goes back as it was before it saved.
EDIT 2: Nothing gets saved.
EDIT 3: I added a serializedObject.ApplyModifiedProperties (); to the end of the script but that did nothing.
If you're changing the scripted object directly trough references to it's variables, like you're doing, serializedObject.Update() and Apply$$anonymous$$odifiedProperties() should not be neccessary. You only need those when you're modifying the object by using SerializedProperties. Try to ditch the .Update() call, it might be preventing the other parts from working, although I doubt it.
If that's not it, all I can think of is some Awake function resetting the values, the values being properties ins$$anonymous$$d of fields (ie. declared as bool allowXRot { get; set; } ins$$anonymous$$d of bool allowXRot;).
By the way, this thing here:
if(theScript.allowRotation == true)
$$anonymous$$akes santa cry. The check (x == true) is completely redundant, as it gives exactly the same result as just using (x). So please;
if(theScript.allowRotation)
@Baste Thanks! That seems to have fixed it.
EDIT: Never$$anonymous$$d. It resets when I restart Unity.
Epic bump - this is at the top of Google and the docs are horrendous.
$$anonymous$$ake allowRotation
field serializable with adding [SerializeField]
attribute before decleration of .allowRotation
Your answer
![](https://koobas.hobune.stream/wayback/20220613174511im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Perform action on save/load in editor 2 Answers
Save Data from Editor Script, Serialize Nested Class List 1 Answer
Don't save gameobjects to scene file (No hide flags) 0 Answers
Why do I "Save Project"? 0 Answers
Saving a scene within the game? 1 Answer