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?

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.

Hello, if you still have your problem, just replace
EditorUtility.SetDirty(target) with EditorUtility.SetDirty(tc)