- Home /
Changes made with custom editor not sticking.
One of my behaviors has a struct in it that I would like to be able to edit, so I made a custom editor for it. This lets me make edit it and make changes, but as soon as I hit the play button to test, my changes vanish. Any idea why the changes don't stick?
Here's a simple test case:
(editor script)
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TestComponent))]
class TestEditorScript : Editor
{
public override void OnInspectorGUI()
{
TestComponent tc = target as TestComponent;
TestComponent.TestStruct ts = tc.TestStructInst;
ts.TestValue = EditorGUILayout.IntField(ts.TestValue);
if (GUI.changed)
{
tc.TestStructInst = ts;
EditorUtility.SetDirty(target);
Debug.Log("Editor set value to " + tc.TestStructInst.TestValue.ToString());
}
}
}
(this one attached to a game object)
using System;
using UnityEngine;
public class TestComponent : MonoBehaviour
{
[Serializable]
public struct TestStruct
{
public int TestValue;
}
public TestStruct TestStructInst;
void Start()
{
Debug.Log("But when playing the value is: " + TestStructInst.TestValue.ToString());
}
}
When changing the value in the editor, that Debug.Log in the editor script tells me I set the value to 100 or whatever like I typed. Once I hit play, the one in the component always says the value is zero, though.
Any idea what I'm missing?
Answer by ExciteMike · Apr 17, 2012 at 12:26 AM
It seems Unity simply doesn't know how to load up the settings for a struct. If I change it to a class instead, it works as expected.
It is not important that it be a struct instead of a class in this case, so I'll just do that for now.
Answer by Matsegui · Mar 05, 2013 at 01:01 PM
Hello, if you still have your problem, just replace EditorUtility.SetDirty(target) with EditorUtility.SetDirty(tc)
Your answer
Follow this Question
Related Questions
Draw inspector of array elements? 1 Answer
Editor performance: "CustomEditorAttributes" in Inspector 1 Answer
Why is ScriptableObject asset not saving to disk? 2 Answers
How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer
animation window custom scripting? Paste curve from one object onto another. 0 Answers