Custom editor, float not saving when enter playmode.
Hi guys, I struggle with saving a float and a toggle and make them not reset when I enter play mode. The float inspected in not part of the class and it is just a visual element for now. Whenever I enter play mode the value resets to 0.
private bool ceva;
float speed;
public override void OnInspectorGUI()
{
serializedObject.Update();
//EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((Bot)target), typeof(Bot), false);
//foldout = EditorGUILayout.Foldout(foldout, "Behaviour Settings");
//EditorPrefs.SetBool("foldout", foldout);
Undo.RegisterCompleteObjectUndo(target, "Something changed");
PrefabUtility.RecordPrefabInstancePropertyModifications(target);
speed = EditorGUILayout.FloatField(speed);
ceva = EditorGUILayout.Toggle(ceva);
if (GUI.changed)
{
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
}
serializedObject.ApplyModifiedProperties();
}
As you can see I tried the set dirty
method but it does not work. I think because the float and the bool are not part of bot.cs than serializedObject.ApplyModifiedProperties
will not change anything. I also checked out this post https://answers.unity.com/questions/1232239/unity-editor-not-saving-object-changes.html and some dude suggested that I should use either undo system or serializedObject
because set dirty is no longer active in unity 4.5 or so. I checked undo documentation and added the line before the actual var modification. Still nothing. Any help is appreciated! Thanks!
Answer by streeetwalker · May 02, 2020 at 06:28 AM
Hi @nickk2002 ... Wait! You're trying to show properties that don't exist on some class?
I don't think your problem is that the editor is not saving changes. The problem is it has nowhere to save them to. No measure you could take would rectify that beyond coding properties for your values.
When the game is run inspectors' values are serialized and in order for that to happen you have to have real properties that exist behind the inspector - in your class code.
So anything you want to show in the inspector has to be saved somewhere and also be serializable if you want the same values to appear again.
I bet you get the same behavior whenever you recompile a script as well... and I'd be surprised if it remembers the cosmetic values when you click on one inspector and then back on the one with the problem.
if it is only "cosmetic for now" why not just put the few lines of code in your class to make them real properties?
Ok, now I understand :). I managed to accomplish this for a small static bool for the foldout using EditorPrefs.SetBool();, But I want to have a list of boolean toggles in the inspector in order to select which actions the bot will have. Is there any other way besides creating a class field? Thanks for the answer :).
It's been a couple of months since I've done a custom inspector, and In $$anonymous$$e I was drawing properties of more than one class. I'm not sure if you could use a static class to hold the values, but ultimately you have to have real variables behind the values somewhere.
Yeah, I don't think EditorPrefs was meant to be used to generally save data - it has limited usefulness in that respect.
I did a matrix of checkboxes that does not actually exist as a property anywhere, but I had to construct it in the custom inspector using the values of an array. $$anonymous$$y representation replaced the list's normal inspector representation. You can see my example about midway down in this thread.
https://forum.unity.com/threads/use-object-picker-to-pick-an-object-from-scene-view.534264/#post-5529370
that is built from a simple 1D array of "room" objects.
You are really pro in custom editors I see. $$anonymous$$y goal is to create a simple 1D List of toggles. ceva = EditorGUILayout.PropertyField(serializedObject.FindProperty("ceva"))
this works. but the list way bot.toggleList[index] = EditorGUILayout.PropertyField(serializedObject.FindProperty("togleList").GetArrayElementAtIndex(index),true);
does not work. The toggle is not clickable at all. Is it because I use List<> ins$$anonymous$$d of normal array? https://pastebin.com/J7Vm19TX here is the entire custom editor.
Thank you for the time spent with on this question, you are a nice guy
Your answer
![](https://koobas.hobune.stream/wayback/20220612233037im_/https://answers.unity.com/themes/thub/images/avi.jpg)