- Home /
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 ();
}
}
}
Answer by LukeZaz · Mar 27, 2015 at 01:16 AM
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);
Answer by Johat · Mar 23, 2015 at 11:19 AM
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.
Didn't work, unfortunately.
public int tilePriority = 1;
t.tilePriority = EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to deter$$anonymous$$e how this tile connects to others. Negatives not recommended."), t.tilePriority);
No change. In fact, I've since added a GameObject variable and it won't save either. I've updated my main post with more information.
Your answer
Follow this Question
Related Questions
Filling list with editor scripts is empty on Play 1 Answer
Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers
How can I get complete path of a sprite? 1 Answer
When my scripts recompile or I enter play mode, my custom class gets reset 1 Answer
Dynamic serialized fields based on enum 0 Answers