Editor script variables resetting after restarting Unity

Most of it is in the title. I have prefabs with a specific script on them, and every time I restart Unity (doesn’t happen when playing the scene) this one variable will reset to its default of 1.

Line for editing the variable in my custom editor:

t.tilePriority = (short) EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to determine how this tile connects to others.  Negatives not recommended."), t.tilePriority);

Line defining the variable itself:

public short tilePriority = 1;

EDIT: I’ve since added another GameObject variable and it won’t save either. Both variables are defined in the main class, not the custom editor. More details:

GameObject variable defined:

public GameObject backgroundTile;

Entire custom editor script:

public class TileTexEditor : Editor {

    public override void OnInspectorGUI ()
    {
        TileTextureManager t = (TileTextureManager) target;
	
	    t.tileType = EditorGUILayout.IntField (new GUIContent ("Tile type: ", "Tile type. used to identify this unique tile.."), t.tileType);
	    t.tilePriority = EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to determine how this tile connects to others.  Negatives not recommended."), t.tilePriority);
	
	    t.backgroundTile = EditorGUILayout.ObjectField ("Background Tile: ", t.backgroundTile, typeof (GameObject), false) as GameObject;
	
	    serializedObject.Update ();
	    SerializedProperty spr = serializedObject.FindProperty ("sprites");
	
	    EditorGUI.BeginChangeCheck ();
	    EditorGUILayout.PropertyField (spr, true);
	
	    if (EditorGUI.EndChangeCheck ())
		    serializedObject.ApplyModifiedProperties ();
        }
    }
}

Found the solution thanks to user WazWaz on reddit.

Solution was to use EditorUtility.SetDirty in the custom editor.

Updated code:

TileTextureManager t = (TileTextureManager) target;

GUI.changed = false;
	
t.tileType = EditorGUILayout.IntField (new GUIContent ("Tile type: ", "Tile type. used to identify this unique tile.."), t.tileType);
t.tilePriority = EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to determine how this tile connects to others.  Negatives not recommended."), t.tilePriority);
	
t.backgroundTile = EditorGUILayout.ObjectField ("Background Tile: ", t.backgroundTile, typeof (GameObject), false) as GameObject;
	
if (GUI.changed)
	EditorUtility.SetDirty (t);

Sounds like a serialization problem to me. Is this variable defined in the MonoBehaviour or in the custom editor? I’m assuming the former from the syntax.

Anywho, Unity doesn’t currently serialize shorts so the value will keep resetting. Changing the variable type to an int should fix your problem. A limitation of Unity, I’m afraid.

You can vote on this feature being supported in future versions of Unity here, but it doesn’t look like it’s coming any time soon.

For anyone wondering if this still works in 2022,
it saved me!
Thank you old yet short thread :slight_smile: