Serialized Custom Editors

I have this problem.
I’ve customized the inspector of “Paloma” class with three sliders. However when i quit focus of the inspector’s gameObject this class is attached to all the changes are gone. Despite all variables are serialized, the values are not saved.

I know that if Paloma would have some variables I could do: Serialized Object= new SerializedObject(target) and from there take out all of the variables with FindProperty… and there would be no problem . But what I want is to use variables created in the PalomaInspector class.
I hope i’ve explained well.

Here is the code:

using UnityEngine;
using System.Collections;
using UnityEditor;

[System.Serializable]
[CustomEditor(typeof(Paloma))]
public class PalomaInspector : Editor
{
	[SerializeField]
	float x,y,z;

	public override void OnInspectorGUI()
	{

		x=EditorGUILayout.Slider(x,0.0f,100f);
		y=EditorGUILayout.Slider(y,0.0f,100f);
		z=EditorGUILayout.Slider(z,0.0f,100f);

	}
}

Unfortunately the PalomaInspector will be destroyed everytime you change seletion and lose focus.
The memory created in the Inspector is mainly throwaway.
If you’re wanting to have persistent data then use a static. (Last resort)

Or put the variables on the target object being inspected. Is that not what you want to do anyway? Have x, y and z on the target object Paloma and the inspector change them per object?

x, y and z here will be new each time the inspector is loaded as it is a new instance of the inspector.

If you’re having values from Paloma then you just need this instead:

   void OnEnable ()
    {
        x = (target as Paloma).x;
        y = (target as Paloma).y;
        z = (target as Paloma).z;
    }

Serialized fields x, y and z correspond to PalomaInspector object, not to Paloma.

You can use serializedObject or serializedObjects property from Editor to access your serialized fields. That’s the easiest way to not lose features such as undo support.

However, you can use the target Paloma object and assign fields straight to it. Also, don’t forget to mark the object as dirty with EditorUtility.SetDirty in order for Unity to track changes on your object. Let this serve as a proof of concept:

public override void OnInspectorGUI ()
{
    EditorGUI.BeginChangeCheck ();
    x = EditorGUILayout.Slider (x, 0f, 100f);
    y = EditorGUILayout.Slider (y, 0f, 100f);
    z = EditorGUILayout.Slider (z, 0f, 100f);
    if (EditorGUI.EndChangeCheck ()) {
        Paloma p = target as Paloma;
        p.x = x;
        p.y = y;
        p.z = z;
        EditorUtility.SetDirty (p);
    }
}