- Home /
Custom inspector for scriptable object resets object
I have a scriptable object that holds a list of objects, and I have a custom inspector for it. When I select the object in the project view it shows the custom inspector however when I make any changes to the custom editor (even just adding an empty line somewhere and saving) it resets the object to default instead of just changing the controls in the inspector.
Here's the inspector code:
[CustomEditor(typeof(NonBranchingConversation))]
public class NonBranchingConversationEditor : Editor
{
public override void OnInspectorGUI()
{
NonBranchingConversation t = target as NonBranchingConversation;
GUILayout.Label("Node Count: " + t.dialogueNodes.Count);
if (GUILayout.Button("Add New Node"))
t.dialogueNodes.Add(new NonBranchingDialogueNode());
for (int i = 0; i < t.dialogueNodes.Count; i++)
{
GUILayout.Label("Node " + (i+1));
t.dialogueNodes[i].text = EditorGUILayout.TextField("Text", t.dialogueNodes[i].text);
Seperator();
}
EditorUtility.SetDirty(t);
}
void Seperator()
{
GUILayout.Label("-----------------------------------");
}
}
And here's the scriptable object:
public class NonBranchingConversation : ScriptableObject
{
public List<NonBranchingDialogueNode> dialogueNodes = new List<NonBranchingDialogueNode>();
}
If anyone can help with this it would be greatly appreciated.
EDIT: After a bit more goggling, I found something that said to nest one of the classes within the other, but after trying both of mine nested within each other it still doesn't work. Any other suggestions?
Try marking both your NonBranchingConversation and NonBranchingDialogueNode as Serializable. Like so:
[System.Serializable]
public class NonBranchingConversation : ScriptableObject
{
public List<NonBranchingDialogueNode> dialogueNodes = new List<NonBranchingDialogueNode>();
}
Thanks for the suggestion, but that didn't change anything
Answer by Utamaru · Aug 07, 2014 at 08:42 AM
Maybe its too late, but use this line of the code, after you changed something in your object:
EditorUtility.SetDirty(target);
Answer by LeftyRighty · Jun 04, 2015 at 12:01 PM
just in case someone else comes across this problem in the future, where by changing the editor script code removes the existing items within the scriptable object.
PAEveson was correct the fix is adding [System.Serializable], but you need to add it to the class(es) the scriptable object is storing, not the scriptable object script itself.
Your answer

Follow this Question
Related Questions
Deep Copy of ScriptableObject 1 Answer
Custom Inspector for ScriptableObject 1 Answer
How much bigger are ScriptableObjects compared to normal classes? 0 Answers
ScriptableObject with Custom Editor resetting data in inspector 1 Answer
Implementing a 2D array as a field on a ScriptableObject? 1 Answer